Text wrap with box in Basemap - python-2.7

I'd like to write some text on a world map pointing to several locations. The text to be written for each location is short, a few lines, with one or two words/numbers (i.e earthquake magnitude, location, date). Is there a pythonic way to perform this?

Since matplotlib-basemap is based on matplotlib, you can simply use
plt.text(x,y,'yourtext')
to write text onto your map. If you want to have it boxed, add something like:
plt.text(x,y,'yourtext',bbox={'pad':10})
See also http://matplotlib.org/users/text_intro.html.

Related

Apply a function to a range of cells in a spreadsheet

The answers in topics with similar titles haven't given me much of a resolution to my particular problem, but possibly I am not asking the right question. It might help knowing I'm an absolute noob when it comes to spreadsheets, so finding my way around is next to nil.
Currently I can set a basic function in the first cell A1 =ROW()
Simple right? Well now here comes the complication. If I click on the bottom right of the cell and start dragging I can then apply that very same function to a whole range of cells. Let's say I apply it from A1:A10. Every cell within this group now has the same function.
Hooray! We did it, right? I applied a function to a range of cells each with their own output. But wait, if I then go back to the original cell and change its formula none of the other cells change with it. GRRRRR!!!!
There are a couple of fixes I've come up with but don't necessarily know how to implement. The first is to have every cell link back to the original cell and reference its function. This would be useful if I wanted to randomly scatter dependent cells about the document. The other would be much more useful in an orderly group where you know the exact dimensions by specifying in the original cell the size of the array you want to apply the function to.
With that said, let me hear your thoughts.
The closest I've come to an answer is to use FORMULA() which returns the formula used by a cell as text. Unfortunately all answers on evaluating the text resort to scripting. How strange! I thought something like this would be common. Might as well get to scripting.
Hold on, I may have spoke too soon. An array can be made with =MUNIT(), but it's only square. Drats!
Ok... I'm hoping the zebra stripes will eventually become its own answer unless someone else beats me to it. So a simple array can be made with ={1,2;3,4} where commas separate values by column and semicolons for values by row except to generate it you have to press Control+Shift+Enter (because reasons?). I'm thinking now that I'll need to have functions that can generate lists of values based on a single function for each row, and pray that it'll work. So, back to looking. (Wow this is taking forever)
The way I was hypothesizing can't even generate a 1x1, e.g., ={ROW()} returns Err:512 which is a formula overflow.
Alright, in summary so far I've narrowed down the two options,
1) link every cell to the original formula
2) populate an array with a single formula
each with their own incomplete answer,
a) use FORMULA() to return the formula of a cell as text
b) create a hypothetical array like so ={LIST_OF_VALUES()}
These both require a strange form of the nonexistent EVALUATE() function to 'function' correctly. Isn't that fun?
Google Sheets handles case b by allowing ={ROW()}Control+Shift+Enter to generate =ArrayFormula({ROW()}). Working with the general case of any sized array being filled with a single function doesn't exist in the world of spreadsheets it seems. That's very saddening because I can't think of a much better tool for what I want to do. Copy paste it is until I need to use macros.
Depending on your specific use case, creating a user-defined function may help:
use the Basic IDE to create your function;
apply it to any cells on any sheet;
modifying the Basic code will affect all cells where the function is used.
I've elaborated the steps in an answer on superuser.
Sure, you could write some complex code to update functions, but wouldn't the easy way be just to drag it to the same range of cells the same way you did before? It should properly overwrite the existing code in there, and if it doesn't, you can just as easily delete the outdated code and drag the new code in.
Probably the best approach is to simply drag the amended formula over the range of cells (as advised by OldBunny2800). This is less error prone and easier to maintain than a custom macro.
Another option would be to use an array function. Then you only have to edit the function once, and the same edit will be automatically applied to the whole range of cells in that array function.

Formatting text in R Shiny

I am developing an application in R Shiny. One of my modules in the application, displays dynamic text depending on user inputs. I would like to display the text as bullet points and additionally would like the text in "strong" or "heading" format. I can do this individually but somehow I am not able to figure out how to combine the 2. So assuming that my function returns a character vector a, with 2 components that I need to display, I can do the following:
HTML("<ul><li>",a[1],"</li><li>",a[2]) #To generate bullet points
HTML(paste(h4(a[1],a[2],sep=''))) #To concatenate and change format to heading
Now my question is - How can I do both (display as bullets with heading format)? I tried different combinations but it's not working.
Secondly, how do I change the colour of my text?
Any help will be greatly appreciated.
Thanks!
To get a bullet list with heading format, you can just add an <h4> tag to the paste. Also, make sure you close all the tags you open, for ex:
HTML("<ul'><li><h4>",a[1],"</h4></li><li><h4>",a[2],"</h4></li></ul>")
You can use inline CSS to change the color, for ex:
HTML("<ul style='color:red'><li><h4>",a[1],"</h4></li><li><h4>",a[2],"</h4></li></ul>")
More info here.

Horizontal stretching in ListRenderer

I have a list that should display 7 items that each look like this:
Date Weekday Distance Time
Long text that may span many lines
two column text Distance Time
two column text Distance Time
two column text Distance Time
The last lines repeat in a number depending on the data, i e there may be different amounts of such lines for each list item.
I have tried implementing this with a ListCellRenderer that creates a table according to the requirements above, but I have a few problems with it:
The long text that may span many lines is implemented in a SpanLabel. But this text will not display more than one line anyway
Each item in the list will get space for the same number of lines below the first two..
So it seems that items in a list must be of the same size.
Later I also want to be able to detect selection on the entire list item, not just individual fields of it.
Is there a better way to do this?
How do I ensure that the SpanLabel actually gets as much space as it needs?
How do I ensure that the unknown number of lines gets the space they need, depending on how many they are?
Don't use a list: https://www.codenameone.com/blog/deeper-in-the-renderer.html
Lists in Codename One assume every entry is exactly the same height and provide no flexibility here.
I suggest doing something like the property cross demo: https://www.udemy.com/learn-mobile-programming-by-example-with-codename-one/
Where we use a Container with components within to provide a list like behavior with the full flexibility that arbitrary components allow.

GTK TextView - creating a static display format

I am trying to simulate a piece of hardware, and this hardware has a static ribbon display.
to do this, I'd like to use a TextView. My display has 10 rows, with 25 columns. So I figured that a TextView should be easy enough.
basically, I would like to be able to say "insert/replace string S at row X, starting at column Y". i may need to only update a specific row, or even a single column within a row.
I have not been successful at getting this to work though. the best I have been able to do is to fill the TextView with 10 lines of 25 spaces when i create it, and then use the get_iter_at_line_offset to get the iterator of a line, and then push the new text onto that line.
but this will start appending text to the line, rather than replacing the existing one.
I need both row and column control (i.e. need to be able to set text at a specific (X,Y) coordinate).
I'm assuming this is somehow possible using marks.
Can anyone give me a quick example of how i can do this? Unfortunately, there isn't a whole lot of documentation on this sort of thing.
You'll have to get an iter at a specific line, row X, and then use the iterator's forward_chars() method to move forward Y characters. Then delete the number of characters you are replacing, and finally insert the text you want to insert. You can do it all with iterators, I think - iterators are invalidated when you change the buffer, but when you delete text, one of your iterators is revalidated to point to the place where the text was.
If you're targetting GTK+ 3.x, you should really look into using Cairo. Since you don't actually need a text buffer, it seems like overkill and a bit of a mis-alignment to use the GtkTextView.
Look at the very basic introduction on how to draw with Cairo in GTK+. Then look at the text-rendering Cairo APIs, that should be enough to get you started.

how to traverse a file in python and c++ in backward way? And also store data in backward (bottom to top) way?

Suppose i want to store 3 lines in a file both in python and C++ .
I want to store it like this
aaa
bbb
ccc ..
But i am giving ccc input first then bbb then aaa. How will I traverse the file from bottom to top and also store from bottom to top/?
It isn't obvious from the title and question whether you want to store to a file, load from a file, or both, so I'll cover both cases:
Reading
If it's OK to load it all into memory at once (in Python):
list(reversed(list(open('foo.txt'))))
Otherwise, it gets a lot more difficult. Processing a file backwards requires that you read blocks of data a time from the end, scanning backwards through each block for newline marker, and stitching things back together at block boundaries.
Writing
If the data all fit in memory at once, put the numbers into a list (in Python):
open('foo.txt', 'w').writelines(reversed(data))
If data is an iterable, replace it with list(data).
If the data doesn't fit in memory (e.g., you have some generator that spits out a ton of data), the problem will be much harder. The simplest solution that comes to mind is to just push the data into a sqlite database and then copy it into the file. Or you might just find it easier to use the data directly from sqlite.
You might want to use a collections.deque. Afaik those things are optimised for insertion at one of their endpoints, so you could read your file as it is and fill the lines into a deque object with its appendleft method ... just a thought. No idea how efficient that would be. :)
Insert the lines to be generated at the beginning of your linear structure (list, vector<string>) each time, then iterate your structure from beginning to end.