MySQL vs Firebird vs SQLite

Question: We will implement a company based ( not open source ) software and I really cannot choose which one I am going to work with. I can see the differences between all of them
here
but I need more specific information, like is really MySql not free for not open source projects (based on this blog) or how about their comparison in deployment?
EDIT :  I need to expand my question. We are just implementing an API to our existing project. This will be used as a standalone program but it is an embedded system project so the deployement is really really important. I know that SQLite is really easy to deploy but I can see there are a few disadvantages. Can you please tell me which one can be better used according to this situation ?
Thank you all
Comment: @mehmetozer: do you need an embedded engine? If not you should definitely consider PostgreSQL as well.
Comment: but who uses mysql as an embedded database? :-)
Comment: You can also take a look for IBM DB2 Express-C. But the free edition will use only two CPU cores and 2GB of RAM, but has no limit on number of users or on database size. For most projects, it is sufficient. You can get other editons and support as you need.
Comment: P.S.: I use Postgres but sometimes a IBM-Logo somewhere looks beneficial ;)
Comment: To improve the question (and to get the more specific information you seek), you should clarify what your requirements are. Currently this question reads like "I want to buy a car, which one should I get?".
Comment: As long as it is build by your company for internal use within that company, you need not bother with wether it's open source or not. You're not distributing the software, so GPL (for MySQL) does not apply.
Comment: +1 for the comparison link. The best I had ever seen.
Answer: For firebird this is here http://www.firebirdsql.org/en/about-firebird/

Firebird is a relational database offering many ANSI SQL standard
  features that runs on Linux, Windows, and a variety of Unix platforms.
  Firebird offers excellent concurrency, high performance, and powerful
  language support for stored procedures and triggers. It has been used
  in production systems, under a variety of names, since 1981.
The Firebird Project is a commercially independent project of C and
  C++ programmers, technical advisors and supporters developing and
  enhancing a multi-platform relational database management system based
  on the source code released by Inprise Corp (now known as Borland
  Software Corp) on 25 July, 2000.
FREE LIKE A BIRD. Anyone can build a custom version of Firebird, as
  long as the modifications are made available, under the same IDPL
  licensing, for others to use and build on.
FREE LIKE FREE BEER. No fees for download, registration, licensing or
  deployment, even you distribute Firebird as part of your commercial
  software package.
Firebird's development depends on voluntary funding by people who
  benefit from using it. Funding options range from donations, through
  Firebird Foundation memberships to sponsorship commitments.
Choosing Firebird and saving or making money by your choice? Show your
  appreciation and encouragement by contributing money in proportion to
  these benefits.
Answer: I can't answer for all of them, but SQLite is entirely free to use in any application at all. It's explicitly been put in the Public Domain; you can get a license if you insist, but you're just getting what you have total right to anyway. For deployment, SQLite is certainly deployed in many places (inside many browsers, many smartphones, etc.) so it's definitely production-quality, but you do need to be in a position  to be able to work with a native code library (SQLite's written in C, and compiles to a library); as it happens, that rules it out of my current projects…

MySQL vs Firebird vs SQLite


Content Moderation: Editorial workflow not installed and broken/missing handler on the moderated content view

Question: I'm having troubles enabling Content Moderation Module (content_moderation) on a Drupal 8.7.8 site.
During installation everything seems fine, drush en content_moderation finishes without errors, workflows module is already enabled before.
After install the editorial workflow is missing, but it's possible to create a new content moderation workflow.
All role permissions are available and the Moderation State widget shows up on nodes of the selected types. But using the widget has no effect, nodes don't get published or unpublished.
The database tables for Content Moderation are created properly (ie. content_moderation_state and three more with similar names).
Also the view moderated_content shows "Broken/missing handler" where the filter criteria "Content revision: Moderation state (exposed)" and "Content revision: Moderation state (<> Published)" are supposed to be.
Clicking the Broken/missing handler fields shows some details for the two broken handlers:
The handler for this item is broken or missing. The following details are available:
id: moderation_state_1
table: node_field_revision
field: moderation_state
relationship: none
group_type: group
admin_label:
operator: not in
group: 1
exposed:
is_grouped:
entity_type: node
plugin_id: moderation_state_filter
Enabling the appropriate module may solve this issue. Otherwise, check to see if there is a module update available.

Second field shows this info:
id: moderation_state
table: node_field_revision
field: moderation_state
relationship: none
group_type: group
admin_label:
operator: in
group: 1
exposed: 1
is_grouped:
entity_type: node
plugin_id: moderation_state_filter

I also can't add any moderation state related fields to the view like "Moderation state". The fields do not show up in the "Add fields" dialog.
I'm having no problems setting up Content Moderation on a vanilla Drupal 8.7.8  it just fails on this existing site (same version).
I already tried reverting the site and reinstalling workflows and content_moderation on different ways, with drush and manually. I uninstalled all custom modules and patches and reinstalled content_moderation.
Are there any modules (except workbench) or settings known to cause problems with Content Moderation?
Any ideas what could be causing the problem or where to start looking?
Thanks a lot!
Answer: The guys over at Drupal's Slack channel pointed me in the right direction. I added detailed troubleshooting infos to the Content Moderation Guide.
If you enable Content Moderation (drush en content_moderation) on a site, that was not installed using Drupal's Standard Profile as a base, you will run into some problems which require a few manual steps before you can use Content Moderation, as detailed in the guide.
Fix missing Editorial workflow
If the Editorial workflow was not created when enabling Content Moderation, you can either manually create a new workflow and assign a content type, that's used with your new workflow or import the Editorial workflow manually.
The template for the Editorial Workflow is:
langcode: en
status: true
dependencies:
  module:
    - content_moderation
id: editorial
label: Editorial
type: content_moderation
type_settings:
  states:
    archived:
      label: Archived
      weight: 5
      published: false
      default_revision: true
    draft:
      label: Draft
      published: false
      default_revision: false
      weight: -5
    published:
      label: Published
      published: true
      default_revision: true
      weight: 0
  transitions:
    archive:
      label: Archive
      from:
        - published
      to: archived
      weight: 2
    archived_draft:
      label: 'Restore to Draft'
      from:
        - archived
      to: draft
      weight: 3
    archived_published:
      label: Restore
      from:
        - archived
      to: published
      weight: 4
    create_new_draft:
      label: 'Create New Draft'
      to: draft
      weight: 0
      from:
        - draft
        - published
    publish:
      label: Publish
      to: published
      weight: 1
      from:
        - draft
        - published
  entity_types:
    node: null
  default_moderation_state: draft

Copy the template to some yml file and import it using drush config:import --partial.
Fix Broken/missing handler on the Moderated Content view
This error occurs, if there exists no moderation workflow that is assigned to a content type. Wether you just imported the Editorial workflow as described above or created a custom moderation workflow, you'll have to make sure it has at least one content-type assigned to it. Just edit your moderation workflow, scroll to the bottom of the page and select at least one content type that is used with this workflow. After you save your changes, the "Broken/missing handler" on the Moderated Content will be gone and the view at /admin/content/moderated will work as expected.


Query SQL for table Column name push into PHP Array 1 Column per Index

Question: This is the code I am using:
$columnQuery = "select COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = 'file_system_profiles'";
$getColumns = sqlsrv_query($conn, $columnQuery);
while ($row = sqlsrv_fetch_array($getColumns)) {
    $tempColumns = ($row[0] . PHP_EOL);
    $selectColumns = preg_split('/\s+/', $tempColumns);
    echo $selectColumns[0];
}

The problem is that $getColumns does end up with my column names in it, but it is one long string with no delimiters between each column name.  That is why I tried to use PHP_EOL to separate them by spaces in $tempColumns.  If I echo $tempColumns I do see all my column names separate by spaces.  Then when I do my preg_split on spaces/tabs/new lines I thought I would end up with the $selectColumns array with 1 column name in each index.  Unfortunately when I echo $selectColumns[0] I still get one long string with no spaces.
I know it may be more work for anyone answering, but if you could give me a clue as to why my code didn't work in addition to any code you provide I would appreciate the deeper understanding.
Comment: What does this result in when you execute this query directly on the database?
Comment: A table with a single column that contains each column name for that table  in its own row.
Answer: The \s regular expression matches newlines, so when you use preg_split() you're discarding the newline that you concatenated.
But none of this is necessary, just print each result on a different line, using
 as the line break in HTML.
$columnQuery = "select COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = 'file_system_profiles'";
$getColumns = sqlsrv_query($conn, $columnQuery);
    while ($row = sqlsrv_fetch_array($getColumns)) {
        echo $row['COLUMN_NAME'] . "
";
    }
}

Comment: That would work for me but ultimately I don't want to just print to the screen, I want it in an array that I can use to manipulate the data through loops and such.
Comment: So push it into an array with `$array[] = $row['COLUMN_NAME']`, what's the problem?
Comment: I think you got confused looking at the output of your script, which made it look like it's one long string. It's no different from any other query, each name is in a different row of results.
Comment: I guess I did get confused looking at the output, everything is working as expected now, thank you.


Is it possible to mod_rewrite the full url or mask the actual url altogether?

Question: I've been looking at the Heroku custom domain feature, which enables to change a url from
subdomain.heroku.com

to
www.whatever.com.

Is something like this possible with mod_rewrite or how is this done? It can't be a simple re-direct, can it?
Answer: No rewriting is necessary.  They achieve this by adding your domain to the list of HTTP Host headers that will be mapped to the handler that also handles your subdomain under their own domain.  In Apache configuration terms, the equivalent configuration is ServerAlias.
Comment: @frequent Well, something like that can get very complicated pretty quickly, and there are a lot of different ways to approach it.  The smooth, scalable way would probably involve some kind of configuration management system, like Puppet or Chef.. but the simplest implementation would probably be to have the user's desired configuration from a web form stored in a database, then have a shell script running as a cron job once every 15 minutes that looks at that database (or a web service) for the config, dynamically builds the Apache configs, then gracefully restarts Apache.
Comment: Ah. So, I could do something similar on Apache?
Comment: @frequent Sure - though to have it be client-initiated, you'd need to have the clients have a mechanism to update that config, then have the Apache service gracefully restart to pick up the configuration changes.
Comment: Yes, I was thinking something like this. Client can "pick" his URL to run on my system. If you have any pointer where to get me started, you'd make my evening, too (made my day already...). Thanks!
Comment: Ok. Sounds good. I will give this a try sooner or later (more later... ) Thanks again for helping out! If I get to it and get stuck, I'll post here, maybe you can chip in again.


Como posso "adiar" a inicialização de uma propriedade?

Question: Existem situações em que a inicialização da propriedade não é possível de ser feita na declaração, o seu valor só é conhecido mais tarde.
Um exemplo disso é, no Android, as referências às views de um layout só poderem ser obtidas após ser usado o método setContentView().
Existe algum modo de "adiar" a inicialização sem a necessidade de declarar a propriedade como nullable?
Comment: *Iniciativa Kotlin! seja mais um colaborador fazendo uma pergunta no SOPT*
Answer: Provavelmente a forma mais simples seria:
public lateinit var prop: String

fun init(param: String) {
   valor = Executa(param)
}

Kotlin tem propriedades de forma muita parecida com as propriedades de C#, com uma sintaxe melhor, então pode parecer que ali tem um campo, mas é uma propriedade, mesmo que não tivesse o lateinit.
Coisas como setContentView() são uma impedância de Kotlin com Java porque usa o jeito simulado de propriedade do Java. Até teria como solucionar isso mesmo que não de forma simples. Eu acho que valeria o esforço em nome da padronização de estilo na linguagem, faria como TypeScript resolveu a impedância com JavaScript.
Tem várias outras formas. Veja sobre lazy e propriedades delegadas.
val lazyValue: String by lazy {
    println("computed!")
    "Hello"
}

Coloquei no GitHub para referência futura.
Desta forma só quando ela for necessária é que será inicializada. Em geral usa-se isto para algo que pode nunca ser necessário para o objeto e para gerar seu valor pode ter um processamento pesado ou que pode ter uma valor alterado durante o processo entre a criação do objeto e seu primeiro uso.
Desta forma pode usar com val já que a inicialização ocorre tardiamente. Sem o lateinit um val precisa de um valor já que não pode mais ter seu valor alterado. Ele fica em um estado inválido até ser necessário, o que é diferente de ter um valor nulo e depois ter outro valor.


AOSP - How to have an executable run with root access?

Question: I am building an "embedded" system, based on Android, and at the moment I'm trying to implement updating the firmware of an audio module.
 There is a binary which does that, but the catch is that it needs root access to do it ( it can't access GPIOs otherwise ).
So the problem now has shifted to giving the executable ( and just that executable, mustn't do full root) root access.
I've been at this for a few days, and have tried a bunch of things, with no success.
Things I've already tried :

https://stackoverflow.com/a/13892057/3801327 ( I did this, but still no root. I'm thinking its because of No setuid/setgid programs on since Android 4.3 )
Read somewhere that you can create a system service, which you call, and have the code that requires root run there. Followed this guide to create a system service, but it turns out it doesn't have root access.

I have run out of ideas or things to try. What else can I do ?
EDIT
I was not able to make the binary run as root, but was able to run a shell script after boot from init.rc , where there is root access, and export/change the permissions of the specific gpio I needed. This made it possible to access it without root access needed later on.
Comment: I am not an linux expert but have you tried to simply change the owner of your executable(chown) to root via an init.rc file ?
Comment: Check the first bullet point. Adding the entry to android_filesystem_config.h made the root be the owner. ls -l for it returns :  "-rwsr-sr-x root     root"
Answer: Alex Cohn's answer https://stackoverflow.com/a/44456378/215266 is correct but here's more details.
The init process runs as root and can spawn other root processes. You can use a oneshot service that is disabled by default and kicked off by setting a system property to some value such as 1.
For example in https://android.googlesource.com/device/google/marlin/+/refs/heads/android10-mainline-a-release/init.common.rc :
service vendor.power_sh /vendor/bin/init.power.sh
    class main
    user root
    group root system
    disabled
    oneshot

on property:sys.boot_completed=1
    start vendor.power_sh

Programs started by init are not Android services they are traditional command line applications. The above example is a shell script but you can use C/C++ or Java if you like. The am command for example is primarily written Java, if you do adb shell cat /system/bin/am you will see:
#!/system/bin/sh

if [ "$1" != "instrument" ] ; then
    cmd activity "$@"
else
    base=/system
    export CLASSPATH=$base/framework/am.jar
    exec app_process $base/bin com.android.commands.am.Am "$@"
fi

The source for the Am code is here and you can see it has a traditional main method: https://android.googlesource.com/platform/frameworks/base/+/742a67127366c376fdf188ff99ba30b27d3bf90c/cmds/am/src/com/android/commands/am/Am.java
Answer: You can start your app, or service, or executable from init.rc.
Comment: I can't find anything on the document on how to start an app. Also the executable has to be used later after OS boot, so no point of running it now. That leaves the service ? Is this a regular Java System service ? Because based on the link on my question I know how to make a system service on java side, but not in native side ?  Thank you ):


How to solve sigma summation from 1 to infinity

Question: I was reading this paper and I came across this algorithm:

on page 2, which I'd like to compute on a computer (programatically), but I don't know how you'd go about evaluating a summation to infinity.
Answer: Generally, you can't do such a summation exactly without finding a closed form for it. What you can do, on the other hand, is just sum a lot of terms, since that should give a reasonable approximation for the actual value. Note that the summand can be estimated in absolute value by
$$\frac{2hL^2}{\pi^2 d(L - d)} \frac{1}{k^2}$$
The sequence of partial sums
$$\sum_{k = 1}^N \frac{1}{k^2}$$ converges fairly quickly, and a rigorous analysis can tell you how many terms you'd need until the error is less than whatever your acceptable tolerance is.
Answer: I see that this is the solution to a wave equation. It is also the final answer. There is no closed formula for this that I know of, and that is typical of PDEs. The way you program it is by summing as much of it as you can with computational cost in mind. Fix/vary L, k, d, h, and run across time. The more terms you use, the better your approximation will be at various points $(x,t)$. In the past I wrote alot of Mathematica visual simulations for solutions to PDEs just like these.
As T. Bongers says, this thing converges really fast. If I were writing a live visual simulator I might start by just looking at the first 25 terms, then consider the cost/benefit of adding more terms.


Set of orthogonal vectors in $\mathbb{R}^n$

Question: How can we show that a set of pairwise orthogonal vectors in $\mathbb{R}^n$ has size at most $n$? I know it seems very intuitive, but not sure what the formal proof would look like (whether "elementary" or not).
Answer: Take a linear combination of any set of $k$ vectors with this pairwise-inner-product property. Suppose this linear combination  happens to equal zero. Dot it with each of them in turn to show that each coefficient is zero. Hence the vectors are linearly independent.
In an $n$-dimensional space, a linearly independent set has at most $n$ elements, hence $k \le n$.
Note though, that the "each coeff is zero" step requires that the elements of your set all be nonzero. Otherwise the set $\{ (1, 0), (0, 1), (0, 0) \}$, all of which are pairwise orthogonal (at least by the "orthogonal means dot-product-is-zero" definition), would violate your conclusion.
Answer: You can show that any set $\{x_1, ..., x_m \}$ of orthogonal vectors is a linearly independent set by noticing the following:
$(\sum_{j = 1}^m a_jx_j, x_k) = a_k$ for any $k$, so that if $\sum_{j = 1}^m a_jx_j = 0$ then $a_k = 0$ for all $k$, where $( , )$ is the inner product you are using in $\mathbb{R}^n$.
Then you just have to use the fact from linear algebra that you can find in any book that $n$ is the highest cardinality of any linearly independent set in $\mathbb{R}^n$.
Answer: Consider $n$ orthogonal, or however independent, vectors, then the corresponding $n$ x $n$ matrix has a non null determinant.
Thus a $(n+1)$th (non-null) vector can always be expressed as a linear (non-null) combination of the others, i.e. you cannot find a $(n+1)$th independent vector.


Linear vs Non-Linear

Question: I am attempting some practice problems and I am not sure how to tell if a differential equation is linear or non linear.
The first problem: $$5\left(\frac{dx}{dt}\right)+5x^2+3=0$$
This is non Linear. Could someone explain that to me?
The second was $$y^3\left(\frac{d^2x}{dy^2}\right)+3x -\left(\frac{8}{y-1}\right)=0$$
This one is linear. Could someone explain that to me? I thought the $\frac{8}{y-1}$ would make it non-linear.
Answer: A linear equation only involves the derivatives of the unknown function, eventually multiplied by continuous functions. In particular, you can't have powers of the function, or of its derivatives (example: the equation $x'(y)^2=x(y)$ is not linear).
Your first example is not linear because you're taking $x$ to the power of two.
In your second example, the function $1/y-1$ is continuous -and so is $y^3$- so your equation is linear.
Comment: Assuming $x$ is your unknown function, yes, it'd be non-linear
Comment: In a differential equation, you have to find a function. That function depends on an independant variable. While the notation you used in your question is valid ($x$ for the unknown funtion, $y$ for the variable) it's not very common, from what I've seen. Usually the funtion is noted $x$ and the variable $t$, or the funtion is noted $y$ and the variable $x$.
Comment: Ah I see. So if a differential equation had something like e^2x, would the equation be non linear?
Comment: ok is a unknown function the independent variable?
Comment: oh ok, so the ode such as x(d^2y/dx^2)+3x-(dy/dx)=e^3x would be non linear?
Comment: No, that ode is linear, you're basically multiplying the derivatives of $y$ by continuous functions. The ode $x(d^2y/dx^2)=e^{3y}$, however, would be non linear.
Comment: ok thank you so much.

Linear vs Non-Linear


Big O and running time of specific method given $\Theta(n)$ information on inner function

Question: If evaluating $f(n)$ is $\Theta(n)$
<code>i = 1;
sum = 0;
while (i <= n)
       do if (f(i) > k)
            then sum += f(i);
          i = 2*i;
</code>
Would the running time of this be $O(nlogn)$ because i is doubled after each iteration and the function is called the same number of times? Or could it even be a linear case?  I am very lost on this...
Comment: Your indendation is a little off. If $f(i) \leq k$, is $i = 2i$ still executed?
Comment: Sorry about that. I am assuming it is done in the while loop. The question was posed as if it were lined up with the if though.
Answer: The worst case is if $f(i) \gt k$ is always true, so you can delete that from the code except that you compute $f(i)$ twice per loop.  You are correct that you do $\log_2 n$ passes through the loop, so you do $2 \log_2 n$ evaluations of $f$.  The final result is $f(1)+f(2)+f(4)+f(8)+\ldots f(2^k)$ for $k$ the smallest value where $2^k \gt n$  The sum of the arguments is about $2^{k+1} \approx 2n$ and the total time is about $4n$.  The point is that the early passes are called with small values of the argument, so presumably they run faster than $\Theta(n)$


Why does df say my EFS disk has a capacity of 8 Exabytes?

Question: This is a screenshot from my AWS EC2 instance, which the EFS disk is mounted on:

How can this be?
Comment: Pretty much, AWS says it's unlimited.  I'm guessing there has to be some top end limit but thousands of petabytes is possible.
Comment: That's normal, what specifically are you questioning?
Comment: @kenlukas The disk is literally that big?
Answer: EFS claims it can store "petabytes of data", expanding automatically. AWS has likely decided that 8 exabytes is the number they want to present to the operating system as free space. I can't see any advantage to presenting a smaller number, as some software wants to know free space available on a disk.
From the FAQ
Q. How much data can I store?
Amazon EFS file systems can store petabytes of data. Amazon EFS file systems are elastic, and automatically grow and shrink as you add and remove files. You do not provision file system size up front, and you pay only for what you use.
Comment: That's a good thing since its easy to end up with duplicate data.


Secure upload of big files to Ubuntu server

Question: I need to allow my users an easy way to create a directory and upload a few files to this directory on my Ubuntu server. I also need this to comply with the following requirements:

Must use the local users for authentication (would also be nice if it can be just a subset of these)
Everything uploaded must be uploaded as a specific system user
It must handle files larger than 2gb
It must lock the user to only the root directory that is used for upload
All connections must be secure
Would be nice if partial uploads can be resumed

My initial thought was to setup an FTP server, but I have had hard time finding an FTP server that can be configured to meet these requirements.
Does anyone have suggestions to either what FTP server software that can be configured to meet my needs or a different approach that would meet my needs?
Answer: A secure FTP server is the way to go.
Just try pure-ftpd or vsftpd with explicit or implicit SSL encryption for both data and control channels. You can configure both to fulfill all your requirements.
Answer: ProFTP can do what you are looking for.
See http://www.proftpd.org/docs/directives/configuration_full.html for ProFTPDs config options.
Are you users internal on a lan? If not, can you use a VPN connection for the security?
If not, you can setup FTPS http://proftpd.open-source-solution.org/docs/howto/TLS.html


drupal_get_form renders blank page

Question: I'm building a module for basic e-mail sharing that will present a form on every node. So I hook into nodes and add a new variable called share_form which is supposed to contain the from from email_share_node_form after it has been fetched with drupal_get_form.
I have been checking out loads of examples and as far as I can see I'm doing everything right. But the page turns completely blank, with no errors either in the reports or on the page. The same occurs when I use other form_ids such as user_register_form that is supposed to work.
If I add any normal string to share_form the page renders with no issues at all. So the error is definitely occuring inside drupal_get_form.
Is there obvious error in my code? How can I debug this?
UPDATE: If I put the render(drupal_get_form('email_share_node_form')) in my node.tpl.php it renders just fine. Why not in the module?
/**
 * Hook into node_load to insert an e-mail share form.
 */
function email_share_node_load($nodes, $types) {
  foreach ( $nodes as $nid => $node ) {
    $nodes[$nid]->share_form = drupal_get_form('email_share_node_form');
  }
}

// --------------------------------------------------

/**
 * Create a share-form to display on a node.
 */
function email_share_node_form($form, &$form_state) {
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit')
  );

  return $form;
}

Thanks
Answer: If you have a white page then there is very likely an error somewhere. Check your apache or php error logs. Which could really be any where depend on what platform you are developing on.
Fatal errors won't make it into the database log file within Drupal since fatal errors tend to stop everything dead.
Comment: I get nothing in the PHP error log. But in the Apache log I found `[Fri Aug 12 11:53:38 2011] [notice] child pid 8421 exit signal Segmentation fault (11)`
Answer: This is so late... Hope it helps someone...
I had the same problem with a white page and no errors reported.
In the form function you need need to return drupal_get_form(
I was simply calling drupal_get_form( and not returning it and had the exact same problem.
Comment: Hi and welcome to Druapl Answers. Thanks for the answer, could you please edit it and elaborate.

drupal_get_form renders blank page


Python Dictionary key iteration

Question: Hi Guys im looking to print out a dictionary. I have a 1000 lines i need to print.
The following code gets data in a json. I then put it into a dict. It then prints
out the first line or key 0 in Data.
I want to print up to line 1000 but i want to do it without typing it a 1000 times :)
import time, json, requests

json_api = requests.get("https://www.cryptopia.co.nz/api/GetMarketHistory/ETN_BTC/72")
json_data = json_api.json()

print(json_data['Data'][0]['Price'] , json_data['Data'][0]['Timestamp'] )

This gives output:
4.35e-06 1510454994
Which is correct.
Now i want to print out results for
print(json_data['Data'][1]['Price'] , json_data['Data'][1]['Timestamp'] )
print(json_data['Data'][2]['Price'] , json_data['Data'][2]['Timestamp'] )
print(json_data['Data'][3]['Price'] , json_data['Data'][3]['Timestamp'] )

But without having to write this a 1000 times.
Thanks
Answer: Just use a loop!
for i in range(1, 1000):
    print(json_data['Data'][i]['Price'] , json_data['Data'][i]['Timestamp'] )

Comment: thanks, i didnt realise you could use [i] when printing dictionary key.

Python Dictionary key iteration


what is the root of this polynomial?

Question: Let
$f_n(x)=\prod\limits_{i=1}^n (x+i)-n!=(x+1)(x+2)\cdots(x+n)-n!$
$n$ is a positive integer.
What are the roots of the polynomial for a given $n$ except $0$?
Or determine the real part of the roots or even determine the maximum real part of the roots except $0$.
Thanks!
Comment: I think this only has 1 real root at x=0, since any x greater than 0 would result in the product being greater than n!. Can't say anything about the complex roots.
Comment: @SpencerKraisler Don't we have a root at $x=-n-1$ in case $n$ is even?
Comment: Um.... your function of "$x$" is independent of $x$.
Comment: Thanks, I have fixed this.
Comment: Are you expecting a closed form to describe the general n-th root? or are you looking for some values (how many)? Do you want imaginary roots as well?
Comment: I am more interested in the real part of the roots or even the range of the real part of each root.
Comment: @EdG You are correct, zeroes exist at the even values of $i$ and each is  $x=(-1)(i)-1$, for the odd values of $i, x=0$. This is all about real roots. Have not investigated the complex roots.
Comment: Yes we do. My bad.
Answer: There are always $n$ complex solutions to degree $n$ polynomial, thanks to the fundamental theorem of algebra. I will only provide real solutions here. Note that:
For $x>0$, the value is always greater than zero.
For $x<-n-1$, the absolute value is always greater than zero.
For $0>x>-n-1$, the value can't be zero.
Since $x=0$ is one real root and $x=-n-1$ is the other real root if $n$ is even, the polynomial has one or two real roots.
Attached are cases when $n=3$ and when $n=4$:
Comment: I think you mean $0 > x > -n-1$.
Comment: Thanks! Is it possible to determine the real part of these roots?
Comment: Yes, Can we be more specific? like to find the maximum real part of the roots.

what is the root of this polynomial?


IBM Worklight - cannot change web.xml of the customized .war file prior to deployment

Question: I am using IBM Worklight Developer Edition.
I want to change the web.xml of the customized war before getting deployed but I cannot do that because it is auto deployed and the web.xml is auto generated by the Worklight project builder. I tried to add Web-Info folder under {worklightProject}\server folder but it is ignored.
I read that we can control this on Consumer/Enterprise Edition by editing the build ant task. Is there an alternate way for the Developer Version?
Answer: The same problem occurs with the Consumer and Enterprise Edition build scripts. The ANT tasks generate the web.xml (in v5.05-5.061) for you and then create the WAR file.
I recommend customizing your build process in Eclipse/Worklight Studio to execute a custom ANT script after the build task is triggered via the Run menu options. This way you can alter the WAR with your own customizations. ANT tasks exist for opening WAR (or any ZIP) archives. After expanded, it's just a matter of performing the customization or including additional files before compressing the changes back into the WAR.
You can find add the custom builders by viewing the project properties -> Builders


What does this instrumentation mean?

Question: I was checking the requirements for a score and saw this.
188.8.131.52-184.108.40.206-strings (126.96.36.199.1) - common woodwind doublings allowed

I still don't know how to read the shorthand for orchestra instrumentation. What does it mean?
Comment: Please add the title and composer of the score. Someone else may have the same question in the future.
Comment: As a string player, I find it odd that it specifies the string forces. I'm used to seeing this notation in communication about *gigs*, saying for instance that we need 3 first violins, 3 seconds, 2 violas, etc., but unless this piece features three-way divisi for both violin sections and etc., it's unusual for the score to make specific requirements about the strings.
Comment: Also the publisher, as the notation isn't standardized, and varies by publisher.
Comment: @AndyBonner - I'm picturing a union rep hiring personnel.  He will want to know exactly how many string players he needs to hire.
Comment: @aparente001 What I mean is, barring either those divisi or some unusual piece with very specific requirements, *the piece* doesn't impose an expectation of how many string players. It's usually more up to the conductor (or more likely the budget). I mean, if you went 2-2-1-1-1 it might be too little to balance that many winds, but I don't know that you couldn't go much larger. Of course, I'm not used to seeing this code on pieces in general, just on gig materials, so maybe it's done as a matter of course and ignored as a matter of course.
Comment: @AndyBonner - I've never seen it!  But I've only ever done one union gig.  Accompanying Glen Campbell in Indianapolis.  What a waste of good string players.
 The pay wasn't even enough to cover the union dues.  The union rep convinced me he would hired me for lot of jobs.....  (But I support unionism in general.)
Answer: This score appears to follow the Boosey & Hawkes shorthand for orchestration, which is of the form:
woodwinds - brass - percussion - keyboards - strings (Wikipedia: Shorthand for orchestra instrumentation)
1 flute
1 oboe
1 clarinet
1 bassoon
1 french horn
3 1st violins
3 2nd violins
2 violas
2 cellos
1 bass
Comment: for completeness, you might like to include that the zeros are for: 0 trumpets, 0 trombones, and 0 tubas.
Comment: @guidot - I would **not** say your answer and Aaron's have 90% overlap or greater - yours currently proposes 5 woodwind slots in a row while his only proposes 4, and his names the shorthand used while yours doesn't. At any rate, my belief is that even answers with **100% overlap** are tolerated; it's just that one of them won't get upvoted as much due to readers also detecting the overlap.
Comment: I find it not a convincing approach, to have this answer with >90% overlap to my answer.
Comment: Do you have an example of a publisher whose approach differs from Boosey?  I've never seen one that differed in any respect important enough for me to have noticed.  (The example of Phantom of the Opera, given in the Wikipedia article, does not count, because it differs on account of its being an example of a work from a different genre entirely with its own conventions of orchestration and notation.)
Comment: @guidot I believe that makes this answer at least 10% better.
Answer: Possible publisher deviations aside, a starting point may be here: Wikipedia
In your case it looks like: flute, clarinet, oboe, cor anglais (?), bassoon
Strings are easier, since the ordering is more intuitive:

1st violin
2nd violin
viola
violoncello
double bass
Comment: Why would the bassoon be separated from the rest of the woodwinds by a dash? It makes more sense that it's one French horn and zero of the other brass - and no number for cor anglais either way.
Comment: @ToddWilcox indeed, cor anglais is typically included with the oboes, as piccolo and alto flute are included with the flutes, various clarinet-family instruments with the clarinets, and contrabassoons with the bassoons, typically with some sort of explicit mention of the specific instruments required.
Comment: I don't buy cor anglais -- too unusual.

What does this instrumentation mean?


Normalize numpy array to min max different form column min max

Question: I have a function that normalizes numpy array to min max values that are in the column itself :
<code>def normalize_function(data):
   min = np.amin(data,axis=0)
   max = np.amax(data,axis=0)
   return (data - min)/(max-min)

Value = pdData[['wave1','value2','value3','wave3']].to_numpy()
Value = normalize_function(Value)
</code>
However, I need to preset my own min max values for each column separately.
Example :   for wave1  min=100  max=300,   for value2  min=2 max=10,   for value2   min=10 max=100. These min max values are different from min max values inside the column. Thanks is advance for help!
Answer: This is my solution so far. Add preset min max values to numpy array, normalize and then remove those added min max values.
<code># Normalization preset
normSetFile = "NormSet.xls"
normSetCsv = pd.read_excel(normSetFile)
normSetDF = pd.DataFrame(normSetCsv).astype(float)
npNormSet = normSetDF.to_numpy(dtype=np.float64)

def normalize_function(data):
min = np.amin(data,axis=0)
max = np.amax(data,axis=0)
return (data - min)/(max-min)

def norm_check(data):
for col in range(0, data.shape[1]):
    for ind in range(0, data.shape[0]):
        if (data[ind, col] < npNormSet[0, col]):
            print('Error    data ' + str(data[ind, col]) + ' at column ' + str(col) + ' index ' + str(ind) + ' is lower than min ' + str(npNormSet[0, col]))
        if (data[ind, col] > npNormSet[1, col]):
            print('Error    data ' + str(data[ind, col]) + ' at column ' + str(col) + ' index ' + str(ind) + ' is bigger than max ' + str(npNormSet[1, col]))

def Normalize(data):
norm_check(data)
data = np.append(data, npNormSet, axis=0)
data = normalize_function(data)
data = np.delete(data, slice(len(data)-2, len(data)), axis=0)
return data
</code>


Pergunta sobre PHP , onde não consigo fazer uma exclusão com uma condição"

Question: então não estou conseguindo acertar a respeito de um código de exclusão onde contém uma condição, sendo ela, caso um curso cadastrado, seja uma chave estrangeira na tabela usuário o mesmo não pode ser excluído da tabela curso.
Vou deixar o print das tabelas abaixo e o código da exclusão que não esta recebendo o valor, caso alguém possa ajudar ficarei grato.
Aqui é o código de exclusão:
<?php

include "../includes/conexao.php";

$idcurso = $_GET['idcurso'];
$curso = $_GET['curso'];
$usuario = $_GET['usuario'];

$consulta = mysqli_query("SELECT * FROM usuario WHERE curso = $curso");
$numregistros = mysqli_num_rows($consulta);

if($numregistros == "0") {
    $sql ="DELETE FROM curso where idcurso = $idcurso";
} else {
    echo "";
}

abaixo é o print das tabelas.
Comment: qual o problema, que erro aparece?
Comment: Apenas , vai para ELSE, não acha o if, duro que não sei pq!"
Answer: O erro é que está faltando a conexão com o banco de dados:
$consulta = mysqli_query($conexao, "SELECT * FROM usuario WHERE curso = '$curso'");

Seu script ficará assim:
<?php

include "../includes/conexao.php";

$idcurso = $_GET['idcurso'];
$curso = $_GET['curso'];
$usuario = $_GET['usuario'];

$consulta = mysqli_query($conexao, "SELECT * FROM usuario WHERE curso = '$curso'");
$numregistros = mysqli_num_rows($consulta);

if($numregistros == 0) {
    $sql ="DELETE FROM curso where idcurso = $idcurso";
    mysqli_query($conexao, $sql); // aqui você deleta
} else {
    echo "";
}

Para abrir uma conexão você precisa usar o mysqli_connect, assim:
$conexao = mysqli_connect( SERVIDOR, LOGIN, SENHA, NOME_DO_BANCO);

Comment: Cara consegui, você é o melhor, parabéns!


Android EditText Decimal Integer Value

Question: I have the following app that has a textview and an edittext. When the value of the edittext changes, it updates a textview's content. Now I want the value to be received as an integer. this is the current app:
package your.test.two;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

public class TesttwoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    EditText input = (EditText)findViewById(R.id.editText1);
    input.addTextChangedListener(new TextWatcher(){
        public void afterTextChanged(Editable s){
            update();
        }
        public void beforeTextChanged(CharSequence s, int start, int count, int after){}
        public void onTextChanged(CharSequence s, int start, int before, int count){}
    });
}

public void update(){
    EditText source = (EditText)findViewById(R.id.editText1);
    Editable sourceData = source.getText();
    TextView destination = (TextView)findViewById(R.id.textView1);
    destination.setText(sourceData);
}
}

Now I want the sourceData editable to be somehow converted to an integer so that I can do a if statement on it as follows:
if(sourceData>255){
  sourceData = 0;
}

I already changed the values of the edittext in main.xml by adding the following lines to editText1:
android:inputType="number"
android:maxLength="3"
android:numeric="integer"

Could someone give me a few guidelines as to how this can be done please. Been googling it but all I end up with is things like:
Integer i = Integer.parseInt(edittext.gettext().tostring());

&
Integer i = Integer.valueOf(String s);

And I don't quite get those to work or understand it. Please help!? :(
Answer: You should be able to use
Integer i = Integer.parseInt(edittext.gettext().tostring());

By replacing i with the variable you want to set the integer to, in your case "sourceData", and by replacing edittext with the name of your edittext box, so "source"
So you'd get something like this:
public void update(){
    EditText source = (EditText)findViewById(R.id.editText1);
    Integer sourceData = Integer.parseInt(source.getText().toString());
    TextView destination = (TextView)findViewById(R.id.textView1);
    destination.setText(Integer.toString(sourceData));
}
}

Comment: Aaah thanx guys! That bit of code was what I was looking for!

    destination.setText(Integer.toString(sourceData));

You really helped me. Thanx!
Answer: Even though you can set the properties in the XML of an EditText to be numeric, that will simply prevent the user from entering other information which is not numeric.
The text in an EditText will always be a String. Nevertheless, you can convert it to an integer and then execute your condition:
public void update() {
    EditText source = (EditText)findViewById(R.id.editText1);
    int sourceData = Integer.valueOf(source.getText().toString());

    if (sourceData > 255) {
        sourceData = 0;
    }

    TextView destination = (TextView)findViewById(R.id.textView1);
    destination.setText(Integer.toString(sourceData));
}

Comment: Aaah thanx guys! That bit of code was what I was looking for!

    destination.setText(Integer.toString(sourceData));

You really helped me. Thanx!

Android EditText Decimal Integer Value


Non-canonical isomorphism $\bigoplus_{\mathbb{N}} \prod_{\mathbb{N}} \mathbb{Z} \cong \prod_{\mathbb{N}} \bigoplus_{\mathbb{N}} \mathbb{Z}$

Question: I am viewing the sets above as $\mathbb{Z}$-modules. It is easy to show that they are not canonically isomorphic: we can view the elements on each side as $\mathbb{N} \times \mathbb{N}$ matrices with entries in $\mathbb{Z}$. The elements of $\prod_{\mathbb{N}} \bigoplus_{\mathbb{N}} \mathbb{Z}$ are such that each row is finitely supported. The elements of $\bigoplus_{\mathbb{N}} \prod_{\mathbb{N}} \mathbb{Z}$ are supported on finitely many rows. The transpose map embeds $\bigoplus_{\mathbb{N}} \prod_{\mathbb{N}} \mathbb{Z}$ into $\prod_{\mathbb{N}} \bigoplus_{\mathbb{N}} \mathbb{Z}$, but this map is clearly not surjective.
I am wondering if there is a non-canonical isomorphism between these two modules. Intuitively, I feel as though these groups are different, but I cannot show this. I think that if they are isomorphic, then the axiom of choice will be needed, but again I haven't found an argument. For example, if we replaced $\mathbb{Z}$ with $\mathbb{Q}$, then there are plenty of non-canonical isomorphisms, since we could just use the fact that each are infinite-dimensional $\mathbb{Q}$-vector spaces with the same cardinality and appeal to the axiom of choice. But over $\mathbb{Z}$ I don't know...
Comment: @AitorIribarLopez Good idea, but isn't $\mathrm{Hom}(\oplus \prod \mathbb{Z},\mathbb{Z}) \simeq \prod \mathrm{Hom}(\prod \mathbb{Z},\mathbb{Z}) \simeq \prod \oplus \mathbb{Z}$?
Comment: If you are trying to prove that they are not isomorphic, maybe you can look at their image from some functor. For example, $Hom(\oplus \prod \mathbb Z,\mathbb Z) = \oplus_{\mathbb Z \times \mathbb Z} \mathbb Z$ is countably generated
Comment: I think you have the descriptions of the two groups as $\mathbb{N}\times\mathbb{N}$ matrices the wrong way round?
Comment: Yes thank you, fixed.
Answer: They are not isomorphic.
In what follows, $\bigoplus$ and $\prod$ will denote direct sums or
products of copies of groups indexed by $\mathbb{N}$, and $e_{n}$ will
denote the element of $\prod\mathbb{Z}$ whose $n$th coordinate is $1$,
with all other coordinates zero.
I will show that there is no surjective homomorphism $\bigoplus\prod\mathbb{Z}\to\prod\bigoplus\mathbb{Z}$.
An abelian group $G$ is called slender if, for every homomorphism
$\alpha:\prod\mathbb{Z}\to G$, $\alpha(e_{n})=0$ for all but finitely
many $n$. The following properties of slender groups are standard
(see, for example, Section 13.2 of Fuchs's 2015 book Abelian
Groups).

$\mathbb{Z}$ is slender (this is just the well-known theorem of
Specker).

(Infinite) direct sums of slender groups are slender. So in particular
$\bigoplus\mathbb{Z}$ is slender.

The image of any homomorphism from $\prod\mathbb{Z}$ to a slender
group is a finite rank free abelian group.

So for any homomorphism
$\alpha:\prod\mathbb{Z}\to\bigoplus\mathbb{Z}$, there is some $n$ such
that the image of $\alpha$ contains only elements of
$\bigoplus\mathbb{Z}$ whose $m$th coordinates are zero for $m\geq
n$. Call the least such $n$ the type of $\alpha$.
A homomorphism $\beta:\prod\mathbb{Z}\to\prod\bigoplus\mathbb{Z}$ consists
of a sequence of homomorphisms
$\beta_{i}:\prod\mathbb{Z}\to\bigoplus\mathbb{Z}$. If $\beta_{i}$ has
type $n_{i}$, we'll say that $\beta$ has type $(n_{0},n_{1},n_{2},\dots)$.
A homomorphism
$\gamma:\bigoplus\prod\mathbb{Z}\to\prod\bigoplus\mathbb{Z}$ consists
of a sequence of homomorphisms
$\gamma_{i}:\prod\mathbb{Z}\to\prod\bigoplus\mathbb{Z}$.
Suppose $\gamma_{i}$ has type $(n_{i,0},n_{i,1},n_{i,2},\dots)$. Then
every element of the image of $\gamma$, considered as an
$\mathbb{N}\times\mathbb{N}$ matrix with finitely supported rows, has
the following property:

There is some $k\in\mathbb{N}$ such that, for every $j$, the $m$th
coordinates of the $j$th row are zero for
$$m\geq\max(n_{0,j},n_{1,j},\dots,n_{k,j}).$$

But one can easily construct elements of $\prod\bigoplus\mathbb{Z}$
that do not have this property, and so $\gamma$ cannot be
surjective, and in particular cannot be an isomorphism. For example, take an $\mathbb{N}\times\mathbb{N}$ matrix
where the $j$th row has a single nonzero entry, in the $N_{j}$th
column, where $N_{j}=\max(n_{0,j},n_{1,j},\dots,n_{j,j})$.
In fact, something more general is true. Contained in Corollary 3 of
Zimmermann-Huisgen, Birge, On Fuchs' problem 76, J. Reine Angew. Math. 309, 86-91 (1979). ZBL0408.20038.
is the fact that the groups
$\bigoplus\mathbb{Z},\;\prod\mathbb{Z},\;\bigoplus\prod\mathbb{Z},\;\prod\bigoplus\mathbb{Z},\;\bigoplus\prod\bigoplus\mathbb{Z},\;\prod\bigoplus\prod\mathbb{Z},\dots$ are pairwise nonisomorphic.


Capturar os dados correspondente ao index no banco

Question: Eu consegui alimentar minha listview com dados do SQLite, porém, eu quero agora capturar os dados (no banco) que cada linha da listview corresponde.
Por exemplo:
Linha 10 exibe Cód. 1 e Nome: Felipe
Linha 13 exibe Cód. 4 e Nome: Giovana
Por exemplo, aí possui 2 linhas da list, o método que eu consegui utilizar, me retornou uma String com a linha completa. Assim que eu clicar em uma linha da listview, eu quero que ela me retorne o código (Cod.4, por exemplo) que está sendo mostrado na String guardada no adapter. Por exemplo, quero clicar na linha 10 e que ela me informe que os dados que ela está exibindo, correspondem ao registro de código 1 do banco.
Como são 2 colunas que estão sendo exibidas, quero uma forma de capturar elas separadamente.
Estou alimentando a lisview assim:
while(data.moveToNext()){
    //coluna 1 do banco

    theList.add("Cód.: " + data.getString(0) + "      " + "Nome: " + data.getString(1));
    ListAdapter listAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, theList);
    listView.setAdapter(listAdapter);
}

Comment: Felipe, tá ruim de entender o que você quer fazer. Pode [edit] a pergunta e tentar ser um pouco mais específico? Aliás, tente dar um título descritivo para sua publicação.
Comment: Sim, farei isso.
Comment: Pra ser sincero, agora eu entendi o que você quer fazer. Acho que só precisa mudar o título e mostrar como você está fazendo pra capturar os dados quando clica, qual o valor retornado e qual o valor que você precisa.
Comment: Em vez de um ArrayAdapter use um CursorAdapter.
Comment: Pessoal, vi em outro lugar que estou alimentando minha listview com String ao invés de objetos, irei alterar aqui
Answer: Você deve implementar o método onItemClickListener(), usando a posição retornada (int position) pelo adapter, recupere a String. Em seguida, utilize Regex para extrair o número que refere ao código e faça a busca que você quer no banco:
private OnItemClickListener mMessageClickedHandler = new OnItemClickListener() {
    public void onItemClick(AdapterView parent, View v, int position, long id) {
        // extrai com regex o codigo
        Pattern p = Pattern.compile("\s(\d+)\s");
        Matcher m = p.matcher(parent.getSelectedItem());
        boolean found = m.find();
        String cod = "";
        if (found){
            cod = m.group(1); // testa se é este grupo mesmo ou 0
        }else{} // Algum tratamento de erro

        // Faça a busca no banco através de uma Asynctask<> por exemplo.

    }
};

listView.setOnItemClickListener(mMessageClickedHandler);

Pronto, a variavel cod tem o código do registro no banco e você pode utilizar para fazer a busca no BD.
PS. Caso queira testar algum padrão regex, utilize este site, eu recomendo.


setInterval em javascript nao soma

Question: Boas, eu tenho o seguinte código javascript e quando eu tento somar o strMessage ele não soma, mas adiciona lado a lado ao valor e assim por diante cada vez que  executa, o que estou a fazer de errado?

function main() {

  var s = 0;
  //Espera um segundo
  setInterval(function() {

$.ajax({
  url: 'teste3.php',
  success: function(strMessage) {

    s += strMessage;
  },
});

document.write(s);

  }, 1000);
}

window.onload = main;
Comment: talvez fosse ter que fazer strMessage um number em javascript
Comment: Converta a string para inteiro.
Comment: perfeito, resultou dei um parseInt() e resultou
Comment: s += parseInt(strMessage); ??
Comment: sim, isso mesmo
Comment: não há numeros decimais?
Comment: nao ,porque????
Comment: se houvesse seria mais conveniente o `parseFloat`
Comment: de qq forma não vamos deixar uma pergunta sem resposta
Comment: hmm ok, por acaso isso se calhar me irá fazer faltamais a frente
Answer: talvez fosse ter que fazer strMessage um number em javascript
s += parseInt(strMessage);

ou com decimais:
parseFloat()

setInterval em javascript nao soma


Redirecting SubDomain To A Custom Address

Question: I need some help because I'm new in this thing so please.
I bought a domain from BigRock and I hosted it on blogger so I want to redirect my subdomain to a link.
I mean any link like my fb profile so I don't know how to do this so help me. I know how to create a subdomain but I dont know how to redirect it to any custom URL.
For example I want to redirect fb.domain.com to fb.com/page
Answer: That would need to be handled via a 301 or 302 redirect on your web server.
Comment: Surely you mean 301/302?
Comment: Yep, it's been a long day.


Number of calls of hook_node_access

Question: I use hook_node_access() to redirect users, if they don't have the right attributes to see a specific node.
While programming I used dpm() to check some variables and was wondering, that dpm() is called 9 times although I just opened a single node.
Well, my question is:
Is it normal, that hook_node_access is called several times for one node, so I should use drupal_static() to avoid duplicate calculations or do I have some disadvantageous loops in my code that I should detect and remove (in hook_node_access are no loops, I mean somewhere else)?
I would welcome every advise!
Thanks, Daniel
Comment: Are you sure it's for the same node?
Comment: Hm. I checked the $node variable and you are right. It is not always the same node. It is also called for all the other node types I have defined and in this case the $node variable just contains the type as string instead of an object.
And altough I just view a single node (e.g. node/123) the $op variable also contains 'update' and 'create' for these types and also for the viewed node. The next thing is, that it seems that this hook is called again after the request, because I also get one message ($op == 'view') after leaving the node to the admin config page for example. How can this be?
Comment: Ok the reason why the hook is called after the page request itself, are the private images in the node. Is there a way to avoid to call hook_node_access for images?
Answer: Next time I should look into the docs first...
According to node_access(), it doesn't consider the returned value NODE_ACCESS_IGNORE, which I return, if the user doesn't violate my permissions, so that other modules still can deny access, if their permissions might be violated.
So the answer of my question is: I should use drupal_static() to avoid duplicate calculations.
Answer: Note that drupal_static only operates within the scope of a single page request.

All functions requiring a static variable to persist or cache data
  within a single page request are encouraged to use this function
  unless it is absolutely certain that the static variable will not need
  to be reset during the page request.

I tend to think that using drupal_static might not help you, unless you can see that Drupal is calling your hook multiple times for the same node within a single page request.
I would say it is common to see a lot of calls to your hook.  For example, Drupal needs to check access for any link you put on a menu - if access is not allowed, then the menu item is hidden.
Another possible approach is to use hook_node_access_records().  Drupal caches the result of this call in the node_access table, so your function will only get called when the node is saved.  See the node access example.
Comment: Well you should be able to redirect on access denied, for example see [this answer] (http://drupal.stackexchange.com/questions/327/how-can-a-module-detect-when-drupal-is-outputting-the-access-denied-page).  For the other part, I think you are telling me that your access decision involves the distance between the user location and the node location.  I have some ideas, but it's hard for me to answer that in a comment - perhaps you could ask a new question (link it in a comment from here) and accept my answer to this one?
Comment: Hehe OK, done.  I'm new to this site and need some rep before I can perform basic actions such as leaving comments, so please help me out if I've helped you.
Comment: Hi thanks for your reply. Yes you are right. I realised, that the private images in the node causes this hook called multiple times. I really would like to use hook_node_access_records. But I need a redirect if permission is denied and I also need to calculate the current distance of the user to the location attached to a node. And I don't see a way to accomplish both features with hook_node_access_records()
Comment: Hi. Thanks for the advise for the redirect. I'm really interested in your ideas with the access records. I have a new question [link](http://drupal.stackexchange.com/questions/154149/deny-location-based-access-to-nodes). I accept your answer here, if we find a solution in the other question ;-)
Comment: Unfortunately your provided options don't fit my requirements. But I gave you +1 for your suggestions here. Thanks, keep it up and you will get your rep!
Comment: Cheers Daniel.  I realise you haven't solved your **overall problem**.  But this specific question seems answered (multiple calls is normal, probably drupal_static won't help).  If you agree, why not to mark it as answered so other people understand the status.  Meanwhile your other questions will help you reach your overall solution.

Number of calls of hook_node_access


Symfony2 Data Fixtures with FOSUserBundle

Question: I want to create som fixtures to my project for testing. I do this in a command line script now, but I discovered the DataFixtures bundle from Doctrine and wanted to test it out.
I have the following code:
<?php
// src/Pan100/MoodLogBundle/DataFixtures/ORM/LoadUserData.php

namespace Acme\HelloBundle\DataFixtures\ORM;

use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\DependencyInjection\ContainerAware;

use Pan100\MoodLogBundle\Entity\Day;
use Pan100\MoodLogBundle\Entity\Medication;
use Pan100\MoodLogBundle\Entity\Trigger;

use FOS\UserBundle\Doctrine\UserManager;

class MockDataMaker extends ContainerAware
{
    /**
     * {@inheritDoc}
     */
    public function load(ObjectManager $manager)
    {
        $userManager = $container->get('fos_user.user_manager');
        //make some users
        $user = $userManager->createUser();
        $user->setUsername('John');
        $user->setEmail('firstname.lastname@example.com');
        $user->setPlainPassword('passord');
        $userManager->updateUser($user);
    }

}

And I get this:
  [InvalidArgumentException]
  Could not find any fixtures to load in:

  - /var/www/path/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/D
  ataFixtures/ORM
  - /var/www/path/vendor/symfony/symfony/src/Symfony/Bundle/SecurityBundle/Da
  taFixtures/ORM
  - /var/www/path/vendor/symfony/symfony/src/Symfony/Bundle/TwigBundle/DataFi
  xtures/ORM
  - /var/www/path/vendor/symfony/monolog-bundle/Symfony/Bundle/MonologBundle/
  DataFixtures/ORM
  - /var/www/path/vendor/symfony/swiftmailer-bundle/Symfony/Bundle/Swiftmaile
  rBundle/DataFixtures/ORM
  - /var/www/path/vendor/symfony/assetic-bundle/Symfony/Bundle/AsseticBundle/
  DataFixtures/ORM
  - /var/www/path/vendor/doctrine/doctrine-bundle/Doctrine/Bundle/DoctrineBun
  dle/DataFixtures/ORM
  - /var/www/path/vendor/sensio/framework-extra-bundle/Sensio/Bundle/Framewor
  kExtraBundle/DataFixtures/ORM
  - /var/www/path/vendor/jms/aop-bundle/JMS/AopBundle/DataFixtures/ORM
  - /var/www/path/vendor/jms/di-extra-bundle/JMS/DiExtraBundle/DataFixtures/O
  RM
  - /var/www/path/vendor/jms/security-extra-bundle/JMS/SecurityExtraBundle/Da
  taFixtures/ORM
  - /var/www/path/vendor/friendsofsymfony/user-bundle/FOS/UserBundle/DataFixt
  ures/ORM
  - /var/www/path/src/Pan100/MoodLogBundle/DataFixtures/ORM
  - /var/www/path/vendor/sonata-project/intl-bundle/Sonata/IntlBundle/DataFix
  tures/ORM
  - /var/www/path/vendor/ob/highcharts-bundle/Ob/HighchartsBundle/DataFixture
  s/ORM
  - /var/www/path/vendor/doctrine/doctrine-fixtures-bundle/Doctrine/Bundle/Fi
  xturesBundle/DataFixtures/ORM
  - /var/www/path/src/Acme/DemoBundle/DataFixtures/ORM
  - /var/www/path/vendor/symfony/symfony/src/Symfony/Bundle/WebProfilerBundle
  /DataFixtures/ORM
  - /var/www/path/vendor/sensio/distribution-bundle/Sensio/Bundle/Distributio
  nBundle/DataFixtures/ORM
  - /var/www/path/vendor/sensio/generator-bundle/Sensio/Bundle/GeneratorBundl
  e/DataFixtures/ORM

My question is: how to create users in the fixtures using FOSUserBundle?
Comment: Your Fixture class does not implement `FixtureInterface` or that is copy-paste typo?
Answer: your class must extends "AbstractFixture" AND implements "ContainerAwareInterface" for access usermanager service !
if your class do not extends "AbstractFixture", is not a fixture ;-)
look for different fixtures interfaces and différent containeraware Interfaces in fosub and symfony for more ways.
Verify the namespace if it is a copy and paste
Sadden for my really not terrible English !
ex : for ordered fixtures
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Doctrine\Common\Persistence\ObjectManager;
class LoadArticleFixture extends AbstractFixture implements OrderedFixtureInterface, ContainerAwareInterface
{
/**
 * @var ContainerInterface
 */
private $container;

/**
 * {@inheritDoc}
 */
public function setContainer(ContainerInterface $container = null)
{
    $this->container = $container;
}
public function load(ObjectManager $manager)
{
    $auteurs = $this->container->get('fos_user.user_manager')->findUsers();
    etc...
}

Answer: Your source file location doesn't match your namespace:
src/Pan100/MoodLogBundle/DataFixtures/ORM/LoadUserData.php vs namespace Acme\HelloBundle\DataFixtures\ORM;
Try setting your namespace to Pan100\MoodLogBundle\DataFixtures\ORM.
As MolecularMan points out you should also be implementing FixtureInterface


How to distribute C++20 modules?

Question: All the literature about modules is quite recently new, and I am struggling with one core concept thing.
When I make my own modules, after the linkage process, does exists a conventional or accepted way of package those modules to distribute them as a library?
Comment: As usual, you deliver files.
Comment: @YvesDaoust do you mean, raw source files, right?
Comment: I meant files. Just files.
Answer: Broadly speaking, the products of building a module's interface (as distinct from the linker-products of compilation, like a static/shared library) are not sharable between compilers. At least not the way that compiled libraries for the same OS/platform are. Compiled module formats are compiler-specific and may not even be stable between versions of the same compiler.
As such, if you want to ship a pre-compiled library that was build using modules, then just like non-module builds, you will need to ship textual files that are used to consume that module. Specifically, you need all of the interface units for any modules built into that library. Implementation units need not be given, as their products are all in the compiled form of the library (unless they are implementation partitions included by interface units).
Perhaps in the future, compilers for the same platform will standardize a compiled module format, or even across platforms. But until then, you're going to have to keep shipping text with your pre-compiled libraries.
Comment: Can you please further explain this point: `you will need to ship textual files that are used to consume that module. `
Comment: @NotJungler'sFault: If someone is writing C++ to use some library, they can't just point the compiler at a `.a` or a `.so` and expect it to work. The compiler needs some C++ declarations that reference things in the compiled binary. These usually took the form of header files, but for modules they would take the form of module interface files. Either way though... they're just text files.
Comment: So, for close the question and point the important thing. What w'd be the approach today? Ship them as module interface units? And then link them? You will still have to produce some kind of `.a` or `.so` library, and then the final user must bundle all the pieces? If you like, please, add this to your response.
Comment: @NotJungler'sFault: I believe that my answer clearly answers all of those questions. I don't know of a clearer way to spell out what to do that I haven't already used in my answer. I mean, it contains sentences like "you need all of the interface units for any modules built into that library". There's not much wiggle room for interpretation there.
Comment: sure. Didn't read carefully this one: `Specifically, you need all of the interface units for any modules built into that library. Implementation units need not be give, as their products are all in the compiled form of the library.`
Comment: Modules are just a fancier precompiled headers. They are compiled once for compilation speedup.

How to distribute C++20 modules?


Linear Algebra Question. Invertibility Problem.

Question: $A$ is a $d*k$ full rank matrix, given d > k. Is the matrix $I-AA^T$ invertible?
Comment: What have you tried?
Answer: Take the $\;2\times1\;$ full rank matrix $\;\binom 10\;$ , then
$$\begin{pmatrix}1&0\\0&1\end{pmatrix}-\binom10(1\;0)=\begin{pmatrix}1&0\\0&1\end{pmatrix}-\begin{pmatrix}1&0\\0&0\end{pmatrix}=\begin{pmatrix}0&0\\0&1\end{pmatrix}$$
Answer: $A A^T$ is positive semidefinite (assuming this is over the reals).  If $\lambda > 0$ is an eigenvalue of $A A^T$,
replace $A$ by $\lambda^{-1/2} A$ to get a counterexample.


How to prevent refactoring when I double-click & paste over a variable?

Question: I regularly copy something, double-click something else (generally a variable), and paste over it with ctrl-v. After installing productivity power tools, occasionally (I haven't been able to pinpoint the exact circumstances) it will open up the header file of the variable I pasted over, and refactor it to what I pasted. How do I prevent this?
Answer: When clicking on the variable with Ctrl held, the power tools will navigate to the declaration of the variable. (Which is a feature that I appreciate)
You have two choices:

do not press Ctrl before you have marked the variable with double clicking
disable the "Ctrl+Click go to definition" feature under Tools > Options > Productivity Power Tools
Comment: ahhh I see! I realize that I often go "ctrl, double-click variable, V", which is what causes it


Differentiating between "exploratory" vs "confirmative" p-values

Question: In the paper Gaus et. al. (2015), two key principles are outlined for interpreting p-values. My question is about the second principle: is it common to make a distinction between exploratory and confirmatory p-values? I haven't found other examples in the literature.
Here is the statement of the principle from the paper:

The second principle is to differentiate between exploratory and confirmative p-values. An
exploratory interpretation of a significant p-value typically establishes a new hypothesis. By contrast, a confirmative interpretation of a significant p-value can be considered as "statistical proof" for a hypothesis established previously.

These sections from later in the paper help to clarify:

In experimental research, one has to strictly distinguish between
two steps: hypothesis generation and hypothesis confirmation. It is
very important that the data for the hypothesis confirmation is
independent from the data used for the generation of the hypothesis.

In principal, an exploratory analysis can only generate hypotheses,
but it can never prove a hypothesis. If an exploratory significance has
been obtained, then a hypothesis is generated. However, descriptive
statistical results and other considerations should be used to decide,
whether further research in order to confirm this hypothesis is
worthwhile.

Furthermore, one needs to remember the saying that a precise
answer (confirmative testing) is only possible for a precise question
(hypothesis)
Comment: IMO their claim is dubious: they are saying that if you take a sample of size 200, and it strongly supports a hypothesis you hadn't previously considered, then you shouldn't treat the hypothesis as confirmed; but if the sample is split into two smaller samples of size 100 each, you can look at the first smaller sample and use it to generate the hypothesis, then look at the second smaller sample and use it to confirm that hypothesis - even though you have the same 200 datapoints available to you in both cases.
Comment: @fblundun Inventing a hypothesis to test after you see the data seems looks risky to me and may often lead to spurious results.  Looking at part of the data first (locking up the rest), developing hypotheses to test based on that exploration, and only then unlocking the other part and only testing solely on that seems a possible approach to avoid accusations of data dredging/snooping/fishing or $p$-hacking
Comment: Here is a related thread: ["Examining the data" before analysis: I still don't know what I'm looking for](https://stats.stackexchange.com/q/354535/237901).
Answer: Interesting article.

is it common to make a distinction between exploratory and
confirmatory p-values?

My short answer is that it is uncommon.
In the abstract authors said that

"No other statistical result is misinterpreted as often as p-value"

I agree. However It seems me that, unfortunately, this article do not help too much.
I'm not familiar with Pharmacology literature but the arguments sound general.
The distinction between Exploratory Versus Confirmatory p-value, what the article is focused on, sound unusual to me. This can be my fault but I never see it in statistics/econometrics books and in Wiky this distinction is not mentioned: https://en.wikipedia.org/wiki/P-value
Explanatory p-value is presented as something useful for "hypothesis generation from data". This sound strange to me. Indeed it seems me that the main source for "hypothesis generation" is theory or any kind of information available at priori (from previous research for example) and in any case before to see the data. It seems me that the correct interpretation of p-value is something like what authors named "confirmative". p-value is for hypothesis testing not "hypothesis generation".
Moreover p-value summarize classical hypothesis testing results. If the above distinction make sense it is so also for: t-stat F-stat, LR ratio, ecc. It is not common.
Indeed the paragraph: "Confirmative testing needs a hypothesis and a level of significance both established a priori" [pag 3], sound good for me.
However other statements like the follow sound me dubious:

In experimental research, one has to strictly distinguish between two
steps: hypothesis generation and hypothesis confirmation. It is very
important that the data for the hypothesis confirmation is independent
from the data used for the generation of the hypothesis. [pag 3]

Actually If the two set of data are independent problems like $p$-hacking go away. However in such situation (independent sets) as is possible that the first set give us reliable information for the second? If it not, why "hypothesis generation" come from first sample and work for the second?
Moreover, statements like following put more doubt yet:

Not all of the significant results are clearly exploratory or
undoubtedly confirmative. Some are somewhere in between exploratory
and confirmative. We propose to consider exploratory and confirmative
as two pole positions with a continuum in between.  ... . However,
such hybrid significance testing should be avoided as far as possible.
[pag 6]

Finally this point seems me important:

A hypothesis can be gained by intuition or through theoretical
considerations, but mostly it is generated by an exploratory analysis
of data. [pag 3]

In my view the point is that theory can be data-driven too. However this do not mean that related/previous p-value change his meaning, is the specific result that change his role. It is a finding in first research and become a guess in the following.


RACSignal and an array of view models, is there a more reactive way?

Question: I use UITableViewController to show discovered devices. They are obtained from SDDeviceBrowser, which scans for them constantly and calls its delegate method each time a new device is found. I created a RACSignal like this:
@implementation SDDeviceBrowser (RAC)
- (RACSignal*)rac_newDeviceSignal {
    return [self rac_signalForSelector:@selector(deviceBrowserDidFindNewDevice:) fromProtocol:@protocol(SDDeviceBrowserDelegate)];
}
@end

I use MVVM pattern: my table view's view model is DeviceListViewModel. It has an array devices, containing child view models which are bound to table view cells. It maps the browser's signal and exposes it for the view controller:
@interface DeviceListViewModel ()
@property (strong, readwrite, nonatomic) NSArray *devices;
@property (strong, nonatomic) SDDeviceBrowser *browser;
@end

//boring initialization ommitted

- (RACSignal *)deviceFoundSignal {
    return [[self.browser rac_newDeviceSignal] map:^id(RACTuple* parameters) {
      SDDevice *device = parameters.last;
      DeviceViewModel *deviceViewModel = [[DeviceViewModel alloc] initWithDevice:device];
      self.devices = [self.devices arrayByAddingObject:deviceViewModel];
      return deviceViewModel;
    }];
}

Then the table view controller subscribes to deviceFoundSignal and inserts a row whenever a new device is found:
[[self.viewModel.deviceFoundSignal throttle:0.5] subscribeNext:^(id value) {
  [self.refreshControl endRefreshing];
  //insert new rows to the table view inside beginUpdates/endUpdates
}];

It is also possible to "reset" the device browser: it clears the list of discovered devices and starts scanning again. However, I couldn't find a nice reactive solution to handle that - I just do the following (in the view controller):
[[self.refreshControl rac_signalForControlEvents:UIControlEventValueChanged] subscribeNext:^(id x) {
  [self.viewModel restartScanning];  //clears the 'devices' array and restarts the browser
  [self.tableView reloadData];
}];

This works, but I think it could be done in a more "reactive" way. Adding new view model to the array inside the map: block looks kinda ugly. Am I missing any  features of ReactiveCocoa that can be used here?
Answer: For assembling the array of devices, check out -scanWithStart:reduce:. With this method, you could start with an empty array, and have the reduce block add each device to the array. For example:
[[[self.browser
    rac_newDeviceSignal]
    map:^(RACTuple *parameters) {
        SDDevice *device = parameters.last;
        return [[DeviceViewModel alloc] initWithDevice:device];
    }]
    scanWithStart:@[] reduce:^(NSArray models, DeviceViewModel *deviceViewModel) {
        return [devices arrayByAddingObject:deviceViewModel];
    }]

This doesn't do much for the "reset" functionality. To combine "adding" and "reseting" into one signal and one scan, I would do the following:
First, find somewhere in your code that you can expose the two pertinent signals, namely -rac_newDeviceSignal and which ever signal represents the events that causes a reset. I'll call this latter signal "resetSignal".
With a signal of device additions, and a signal of resets, I would map them each to "operations" on the array of devices. What do I mean by "operation", basically a block that takes the old array of devices, and returns a new array of devices.
RACSignal *addOperation = [[self.browser rac_newDeviceSignal] map:^(RACTuple *parameters) {
    return ^(NSArray *devices) {
        SDDevice *device = parameters.last;
        DeviceViewModel *model = [[DeviceViewModel alloc] initWithDevice:device];
        return [devices arrayByAddingObject:model];
    };
}]

RACSignal *resetOperation = [resetSignal map:^(id _) {
    return ^(NSArray *devices) {
        return @[];
    };
}]

With these two signals in place, they can be +merge:'d into a single signal, which can then be scanned on like shown up above.
[[RACSignal
    merge:@[ addOperation, resetOperation ]]
    scanWithStart:@[] reduce:(NSArray *devices, NSArray *(^operation)(NSArray *)) {
        return operation(devices);
    }]


monotone functions and pdf's

Question: Could you please show me step by step? Also how does the probability integral transformation come into play? "If the random variable $X$ has pdf
$$
f(x)=
\begin{cases}
\tfrac{1}{2}(x-1)\quad \text{if }1< x< 3,\\
 0 \qquad\qquad\;\, \text{otherwise},
\end{cases}
$$
then find a monotone function $u$ such that random variable $Y = u(X)$ has a uniform $(0,1)$ distribution." The answer key says "From the probability integral transformation, Theorem 2.1.10, we know that if $u(x) = F_X(x)$, then $F_X(X)$ is uniformly distributed in $(0,1)$. Therefore, for the given pdf, calculate
$$
u(x) = F_X(x) =
\begin{cases}
0 \qquad\qquad \;\,\text{if }  x\leq 1,\\
\tfrac{1}{4}(x − 1)^2 \quad \text{if  }1 < x < 3, \\
1 \qquad\qquad\;\, \text{if } x\geq 3.
\end{cases}
$$
But what does this mean?
Answer: $F_X(x)$ is the cumulative distribution function of $X$, given by
$$F_X(x)=\int_{-\infty}^xf(t)~dt\;.$$
Clearly this integral is $0$ when $x\le 1$. For $1\le x\le 3$ it's
$$\begin{align*}
\int_{-\infty}^xf(t)~dt&=\int_{-\infty}^10~dt+\int_1^x\frac12(t-1)~dt\\\\
&=0+\frac12\left[\frac12(t-1)^2\right]_1^x\\\\
&=\frac14(x-1)^2\;,
\end{align*}$$
and for $x\ge 3$ it's
$$\begin{align*}
\int_{-\infty}^xf(t)~dt7&=\int_{-\infty}^10~dt+\int_1^3\frac12(t-1)~dt+\int_3^x0~dt\\\\
&=\int_1^3\frac12(t-1)~dt\\\\
&=1\;,
\end{align*}$$
so altogether it's
$$F_X(x) =
\begin{cases}
0,&\text{if }  x\leq 1\\\\
\tfrac{1}{4}(x − 1)^2,&\text{if  }1 \le x \le 3\\\\
1,&\text{if } x\geq 3\;.
\end{cases}$$
Now your Theorem 2.1.10 tells you that if you set $u(x)=F_X(x)$, then $Y=u(X)$ will be uniformly distributed in $(0,1)$.
Comment: @user43126: You're very welcome.
Answer: You obtain $u(x)$ by integrating $f(x)$.  Note that $\frac{d}{dx}\frac{(x-1)^2}{4}= \frac{x-1}{2}$.
So the probability integral transformation  $Y=u(X)$ is uniform on $[0,1]$ where $X$ has the density $f$.
Comment: Please, at least remind the student that when you say integral you really do mean the integral as in area under the curve instead of just the antiderivative. Your showing that the density in question is the derivative of $\frac{1}{4}(x-1)^2$ almost invites a beginner to use antiderivatives instead of integrals, a very common mistake made by those just learning the subject.
Comment: @DilipSarwate thank you for making that point.

monotone functions and pdf's


Chosen-ciphertext attack como é?

Question: Estou a ler sobre CCA porém não consigo compreender em nenhum site que busco, como funciona este ataque e como possivelmente evitá-lo?
Answer: O chosen-ciphertext attack ("ataque com cifra escolhida") pode se referir tanto a um ataque num sistema de criptografia não autenticado (i.e. que só se preocupa com a confidencialidade mas se esquece da integridade e autenticidade) quanto a um método de criptanálise.
Ataque
A encriptação sozinha apenas garante a confidencialidade da comunicação (i.e. que alguém que esteja observando - eavesdropping - a comunicação, mas não alterando - tampering - descubra o que está sendo comunicado), não sua integridade. Isso significa que, em muitos casos, ainda que o atacante não conheça todo o conteúdo da comunicação, ele pode modificar essa comunicação em trânsito (por exemplo) de modo a obter uma série de vantagens. Em alguns casos, até mesmo a obtenção da mensagem original.
Vou dar um exemplo arbitrário, porém semelhante a alguns cenários encontrados na prática: digamos que você tenha um servidor de mensagens, onde Alice, Bob e Mallory possuam contas. Esse servidor usa a criptografia AES-CBC para comunicar com os clientes, de modo que se Alice quiser mandar uma mensagem para Bob ela tem de fazer o seguinte:

Ela escolhe o destinatário e coloca no cabeçalho: to: roberto. from: alice.;
Ela coloca o conteúdo no corpo da mensagem: mensagem: blá, blá, blá;
Ela escolhe um IV aleatório e faz a criptografia usando uma chave compartilhada entre ela e o servidor (pode ser uma chave de sessão única, negociada durante o login).

O resultado seria (por exemplo):
{"iv":"2e6b036434e9dc857ee34476a618f0cb", "ct":"u0LGWohZ9ce05XkV2A3W2o8xbwp49nHtRXMivpfmULwfDjDMvkjr237fDrXpg/JgTYHkcWu5y3ACxmDGuamyVw=="}

Quando essa string é enviada ao servidor, ele decripta usando a chave comum, lê o nome do destinatário no cabeçalho, e em seguida coloca a mensagem na caixa de entrada do mesmo. O que poderia ocorrer de errado se a integridade da mensagem não for verificada?

Mallory intercepta a mensagem cifrada; ela não sabe seu conteúdo, não sabe a chave de criptografia, mas por alguma razão suspeita que o destinatário é Bob;
Ela pega o IV da mensagem e faz duas operações no mesmo:

XOR desse IV com o que ela suspeita ser o destinatário Bob: IV = IV ^ \0\0\0\0roberto\0\0\0\0\0;
XOR desse resultado com seu próprio nome de usuário: IV = IV ^ \0\0\0\0mallory\0\0\0\0\0;

A mensagem, com o IV trocado, é enviada para o servidor no lugar da original:
{"iv":"2e6b03642be7d28c63e55276a618f0cb", "ct":"u0LGWohZ9ce05XkV2A3W2o8xbwp49nHtRXMivpfmULwfDjDMvkjr237fDrXpg/JgTYHkcWu5y3ACxmDGuamyVw=="}

Quando o mesmo é decriptado, o resultado é:

to: mallory. from: alice. mensagem: blá, blá, blá

Então o servidor coloca na caixa de entrada de Mallory a mensagem que deveria ir para Bob...

var chave = CryptoJS.lib.WordArray.random(128/8);

$("button:eq(0)").click(function() {
  var mensagem = "to: roberto. from: alice. mensagem: " + $("input").val();
  $("code:eq(0)").text(mensagem);

  var iv = CryptoJS.lib.WordArray.random(128/8);
  $("code:eq(1)").text(iv);
  $("code:eq(3)").text(iv);

  var encrypted = CryptoJS.AES.encrypt(mensagem, chave, { iv:iv });
  $("code:eq(2)").text(encrypted);

  var bob = CryptoJS.enc.Utf8.parse("\0\0\0\0roberto\0\0\0\0\0");
  $("code:eq(4)").text(bob);

  var mallory = CryptoJS.enc.Utf8.parse("\0\0\0\0mallory\0\0\0\0\0");
  $("code:eq(5)").text(mallory);

  encrypted.iv.words[0] ^= bob.words[0] ^ mallory.words[0];
  encrypted.iv.words[1] ^= bob.words[1] ^ mallory.words[1];
  encrypted.iv.words[2] ^= bob.words[2] ^ mallory.words[2];
  encrypted.iv.words[3] ^= bob.words[3] ^ mallory.words[3];
  $("code:eq(6)").text(encrypted.iv);

  var decrypted = CryptoJS.AES.decrypt(encrypted, chave, { iv:encrypted.iv });
  $("code:eq(7)").text(CryptoJS.enc.Utf8.stringify(decrypted));
});
.mallory {
  background-color: #FFDDDD;
}

Mensagem: <input value="Blá, blá, blá"> <button>Cifrar</button>

PT:

IV:

CT:

IV:

⊕:

⊕:

=:

PT:

Para evitar esse ataque, é necessário usar um modo de operação autenticado ou - na impossibilidade de fazê-lo - aplicar um Autenticador de Mensagem (MAC) à cifra, de modo que o atacante não consiga modificar os dados encriptados sem que isso seja percebido.
Porém isso não basta: se seu protocolo não for muito bem pensado, mesmo que o atacante não consiga produzir uma cifra "válida" ele ainda pode interferir de forma negativa no mesmo. O vídeo sugerido por @Intruso na sua resposta, por exemplo, fala um pouco sobre o assunto (muito embora este outro vídeo que é parte do mesmo curso exemplifique melhor). Um dos exemplos é o mesmo citado anteriormente nesta resposta, e o outro é uma forma simplificada do ataque POODLE - onde a mera informação sobre por que uma determinada cifra foi rejeitada (MAC incorreto ou padding incorreto) é suficiente para permitir que um atacante decifre toda uma mensagem (submetendo cifras arbitrárias ao servidor e verificando qual mensagem de erro foi retornada, ou quanto tempo demorou pro servidor acusar um erro).
Para a criptografia funcionar é necessário portanto que nenhuma informação seja dada ao adversário tanto sobre a a chave e a mensagem mas também sobre a situação da cifra. Ou seja, quando a decifragem falhar, faça tudo o que tiver ao seu alcance para não deixar o adversário saber por que ela falhou (e isso é bem mais difícil do que parece...).
Criptanálise
No contexto da criptanálise, o CCA em geral tem com o objetivo de descobrir uma chave secreta usada por um adversário. Nele o atacante possui - por um tempo limitado ou ilimitado - a capacidade de enviar cifras (ciphertext) de sua escolha para que o usuário decifre, e receber de volta a mensagem (plaintext) correspondente ou, em um cenário mais limitado, somente saber se a cifra enviada é válida ou não (um "validation oracle").
Em uma de suas variantes, o atacante pode continuar fazendo isso depois de receber a resposta, escolhendo novas cifras a serem enviadas baseado nos resultados atuais (CCA Adaptativo), em outra ele tem de escolher todas as cifras de antemão, e após receber os resultados ele perde a capacidade de executar o ataque (ou seja, ela precisa tentar decifrar a chave somente com a informação que ele possui até então).
O mecanismo exato pelo qual a criptanálise é feita varia de algoritmo para algoritmo. Alguns são projetados de modo a serem resistentes a esse tipo de ataque, outros são reconhecidamente vulneráveis e não participam de processos no qual esse tipo de ataque é esperado (i.e. se projeta o protocolo criptográfico de forma que o atacante nunca possa obter informação útil a partir de cifras arbitrárias).
O ponto chave é que a grande maioria (todos?) dos sistemas criptográficos pode ser "quebrado" (ter sua chave recuperada com menos esforço que um ataque de força bruta) caso o atacante possua um certo número de pares plaintext/ciphertext. Vou dar um exemplo usando a Cifra de Vigenère, uma cifra histórica (i.e. não mais utilizada na prática) que pode ser quebrada com um único par conhecido.

Suponha que General A está mandando mensagens para General B usando uma cifra de Vigenère com uma chave desconhecida. O inimigo de alguma forma intercepta a mensagem e a substitui por algumas letras completamente aleatórias de sua própria escolha, digamos NLLCJOVFXXHMLY. General B decifra isso e obtém AKRUWNBXKWNEYX, que não faz o menor sentido. Pego de surpresa, e não achando que esse disparate vale a pena manter secreto, ele pega um telefone não seguro e chama General A: "O que você quer dizer com AKRUWNBXKWNEYX? Por acaso mudaram a chave sem me avisar?" Mas o inimigo tem uma escuta nessa linha, e agora sabe que NLLCJOVFXXHMLY decifra para AKRUWNBXKWNEYX. Ele pode então subtrair os dois textos (cada um sem sentido algum por si só) para obter MATHMATHMATHMA, e agora ele conhece a chave [podendo interceptar futuras comunicações no canal previamente seguro].

Fonte: essa resposta no crypto.SE
A resistência à criptanálise via CCA é importante porque, na prática, é muito difícil evitar que um atacante descubra alguns pares de mensagem/cifra. Um exemplo seria eu te mandar um arquivo por e-mail, você salvá-lo encriptado em algum lugar e eu depois dar um jeito de observar a cifra resultante. Como fui eu que te mandei o plaintext, eu saberei que aquele PT corresponde àquele CT. O exemplo acima é "forçado", mas demonstra a necessidade dessa característica para um algoritmo de encriptação ser considerado seguro.
Answer: CCA é um padrão de ataque, o modo como você pode executar ele geralmente depende do cenário que está tentando proteger (ou atacar).
Um vídeo com os conceitos básicos pode ser encontrado aqui:
https://www.youtube.com/watch?v=AHQySAd8yvQ
Comment: Boa sugestão, mas eu vejo dois problemas com sua resposta: 1) ela é [pouco mais que somente um link](http://meta.pt.stackoverflow.com/q/42/215); o ideal seria que o conteúdo relevante estivesse na própria resposta, e não somente num recurso externo. 2) Embora qualquer referência adicional seja útil, não podemos assumir que os usuários *desse site* tenham domínio do inglês (ainda que muitos de nós tenhamos), e o vídeo linkado está nesse idioma, sem legendas.

Chosen-ciphertext attack como é?


Display an image in Android Studio using ImageView in layout preview

Question: I'm working on an Android app. I create an item layout for a recycler view and I'd like to display a preview of it in Android Studio using layout preview with tools namespace. To accomplish this task I:

Put in sampledata an image (sampledata\nre1.png). This is an important thing because I don't want to include the sample image in the apk.
Use tools namespace in the ImageView to display the image in the layout preview

An screenshot:

And the layout XML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="8dp"
    android:paddingTop="8dp"
    android:paddingRight="8dp"
    android:paddingBottom="8dp"
    tools:context=".ui.adapters.BarcodeAdapter">

    <ImageView
        android:id="@+id/item_barcode_nre1_image"
        android:layout_width="wrap_content"
        tools:layout_width="120dp"
        android:layout_height="112dp"
        android:scaleType="fitXY"
        android:layout_marginStart="@dimen/text_margin"
        android:contentDescription="NRE1"
        android:layout_marginTop="4dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/item_barcode_nre_label"
        tools:src="@sample/nre1.png" />

    <ImageView
        android:id="@+id/item_barcode_nre2_image"
        android:layout_width="wrap_content"
        android:layout_height="112dp"
        android:scaleType="fitXY"
        android:layout_marginEnd="@dimen/text_margin"
        android:contentDescription="NRE2"
        android:adjustViewBounds="true"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@+id/item_barcode_nre1_image"
        tools:src="@sample/nre2.png" />

    <ImageView
        android:id="@+id/item_barcode_codice_fiscale_image"
        android:layout_width="wrap_content"
        android:layout_height="112dp"
        android:scaleType="fitXY"
        android:contentDescription="Codice fiscale"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/item_barcode_codice_fiscale_label"
        tools:src="@sample/codice-fiscale.png"  />

    <TextView
        android:id="@+id/item_barcode_nre_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/text_margin"
        android:text="@string/item_qrcode_nre"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/item_barcode_nre1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"
        android:ellipsize="end"
        android:maxLines="10"
        android:textAppearance="@android:style/TextAppearance.Material.Body1"
        app:layout_constraintEnd_toEndOf="@+id/item_barcode_nre1_image"
        app:layout_constraintStart_toEndOf="@+id/item_barcode_nre_label"
        app:layout_constraintStart_toStartOf="@+id/item_barcode_nre1_image"
        app:layout_constraintTop_toBottomOf="@+id/item_barcode_nre1_image"
        tools:text="1300A" />

    <TextView
        android:id="@+id/item_barcode_nre2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:maxLines="10"
        android:layout_marginTop="4dp"
        android:textAppearance="@android:style/TextAppearance.Material.Body1"
        app:layout_constraintEnd_toEndOf="@+id/item_barcode_nre2_image"
        app:layout_constraintHorizontal_bias="0.482"
        app:layout_constraintStart_toStartOf="@+id/item_barcode_nre2_image"
        app:layout_constraintTop_toBottomOf="@+id/item_barcode_nre2_image"
        tools:text="4003535379" />

    <TextView
        android:id="@+id/item_barcode_codice_fiscale_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/text_margin"
        android:text="@string/item_qrcode_codice_fiscale"
        app:layout_constraintStart_toStartOf="@+id/item_barcode_nre_label"
        app:layout_constraintTop_toBottomOf="@+id/item_barcode_nre1" />

    <TextView
        android:id="@+id/item_barcode_codice_fiscale"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:layout_marginTop="4dp"
        android:textAppearance="@android:style/TextAppearance.Material.Body1"
        app:layout_constraintEnd_toEndOf="@+id/item_barcode_codice_fiscale_image"
        app:layout_constraintStart_toStartOf="@+id/item_barcode_codice_fiscale_image"
        app:layout_constraintTop_toBottomOf="@+id/item_barcode_codice_fiscale_image"
        tools:text="CF" />

</androidx.constraintlayout.widget.ConstraintLayout>

I think I do it correctly, but it does not work. Any idea? Tnx.
Answer: <ImageView
    android:id="@+id/item_barcode_nre1_image"
    android:layout_width="wrap_content"
    tools:layout_width="120dp"
    android:layout_height="112dp"
    android:scaleType="fitXY"
    android:contentDescription="NRE1"
    android:layout_marginTop="4dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/item_barcode_nre_label"
    tools:src="@drawable/ic_launcher_background" />

Working on my side. Let's try. Thanks
Comment: Me too. Try to use an image on the `sampledata` directory.