How to display real-time customised text in customised format? - python

I have (financial) data that I get in real time using an API and I'd like to display it in a customised manner (a bit like the result of a javascript code). For example, if I want to display 10x10 prices and update them as I receive the data and customise them to be green if it is higher than the previous price, red if lower or so, how should I do, what should I use?
I assume there exist a way to do so using python, but I can't formulate my demand briefly so I only get results that confuse me more using search engines...
Could someone help me by explaining where I can get started with that?

I'll give you an overview because what you want is a generalized approach and most UI packages (if not all) should be able to handle this. First, you need to pick a package to write your UI with. There are a number of these available for Python: see here. I'm not sure what your other requirements are so you'll have to choose the one you want yourself. Once you've picked it out, you'll basically go through and create a grid structure composed of individual cells. Each cell will contain a currency value. You'll then add an event for each cell that captures an "on-change" event for the value in the cell. If the new value is greater than the old one, you color it green. If it's less, color it red. You may also want to add a timer for each cell so that the color fades after a period of time.

Related

Dealing With "0" Values in Datadog

I am currently building a set of multiple graphs for my personal company using Datadog. I love how it works but there is only one thing I have not been able to sort out. Whenever my data is generated every 5 minutes, there are times where one or multiple values will come in at '0' which is what I want. The problem is Datadog is for some reason not taking these values into account and so until that same value finally comes in with something other than '0' then nothing will show up saying the value was '0' and then it changed to something else. Instead the graph chooses to create a straight line from the last recorded non-zero value straight to the newest non-zero value. I would love to know how I can get Datadog to consider the zeros and graph them.
In addition, if possible, I would also love to know how I could say something like "if this previous value existed and then on the next set of data it does not show up at all (not even as "0") just assign a "0" to it until it once again appears on the data". Of course for this to be looked into I would need the first problem dealt with.
Here is an image of how it is looking right now which is NOT how I wanted to look. The Red line shows where all the "0" values land, the Green boxes show the last recorded non-zero values.
Example of '0' values not being graph properly
I have tried looking through most of the documentation of Datadog as well as their posted YouTube videos with no luck. They for some reason do not address this even when it is in front of them when showing examples. I expected to find some info online but there seems to be little resources at the moment. This resulted in me thinking this could be the best place to finally get an answer.
I believe you are looking for Interpolation. There are a couple of use cases you specified, you may have to experiment with a few of the options depending on what your data looks like. For example Fill Zero satisfies one of them.
Datadog Graph Functions

Best way to save a 2D Pygame platformer level tile map with extra data

I'm making a 2D platformer using my own engine in Python/Pygame and have made a good start. I've also made a level designer that exports the level tile map and the game imports it, but I need to associate different things, like switches that open specific doors (or to be more precise, pressure plates that hold a specific door open) but my tile map array currently only holds the tile image index number. What's the best way to include associated tiles (like which switch opens which door etc)?
Do I make an extra file with that data? Or do I have 2 values for each tile? I've tried Googling, but it's not really covered anywhere. I'm sure there's someone with this kind of experience out there... I don't really want to hard-code it in as I want the game to be as versatile as possible.
I would change your file format from storing one tile index per 2D cell to storing some more complex data object. My first thought would be a dictionary per cell for maximum flexibility moving forward, but serializing that and storing it will be quite large. There's a trade-off here between flexibility and storage size.
Another option would be using NamedTuples to store a fixed number of parameters per cell, while preserving a concise serialization. NamedTuples are nice because they let you very concisely represent a data object in a way that both serializes well and can be queried into using named fields.
The questions you need to ask yourself are "what metadata do I need to know about each cell on the map" and "how much do I care about concise file size to represent them".
The answer to my question was posted by #BowlingHawk95 as using NamedTuples for the data object which enabled me to add multiple fields for each cell. I wanted to post a sample to show the resulting code, and a snap shot of how I've implemented it to help anybody else looking for the same thing.
# Initialise the level data array with NamedTuples;
# 'linked_point' is a tuple (x, y) for an associated cell - e.g. switch associated with a door:
Cell = namedtuple('Cell', ['image_id', 'linked_point'])
level_data = [[Cell(image_id=0, linked_point=(0, 0)) for _ in range(grid_width)] for _ in range(grid_height)]
And now that I am able to add coordinates (as the linked_point) I can now reference another cell from the one I'm on. The following image shows a shot of my level designer, with the coords in the title bar, and also showing the image_id name and coords of the linked cell.
Massive thanks to #BowlingHawk95 for the assistance!

How to change indent on bulleted list on PPT slide using pptx python library

I have started using 'pptx' library to programmatically generate some PPT based report. We have a company template which I am using as a base. It contains a master slide with bulleted list in single column format which looks like this (but with different formatting for each level such as bullet type and color. Sorry cannot share actual pic here):
Bulleted list format
I wanted to know if there is a way to change the position of the various levels as we would change from the ruler (or Right Click > Paragraphs > indentation) in MS PowerPoint..? If I change "level" parameter of the bulleted list it changes from first level to second level along with all the formatting.
shape.text_frame.paragraphs[0].level = 2
But I would just like to shift the first level by a few centimetres towards right. So it would look like:
Bulleted list shifted to right
I know one obvious way to achieve is by adding new master slide with required format and using that. But if one needs to change indents multiple times, it would be difficult to add new master slide for each specific case. Thus I am searching if there is a way to do in programmatically using 'pptx' functionality. So far I was unable to find such feature in 'pptx', but I might have missed it may be or there is possibly some workaround. If so I would be interested to know.
The short answer is "no". The current version of python-pptx does not support bullet formatting in its API.
To do this you would need to manipulate the underlying XML directly. You can search on "python-pptx workaround function" to get some examples.
I believe the element of interest is p:sp/p:txBody/a:lstStyle, containing elements like "lvl1pPr" and "lvl2pPr" and so on.
A reference to the p:txBody element is available on Shape.text_frame._element

Game map from Code

It's a long one so you might want to get that cup of tea/coffee you've been holding off on ;)
I run a game called World of Arl, it's a turn based strategy game akin to Risk or Diplomacy. Each player has a set of cities, armies and whatnot. The question revolves around the display of these things. Currently the map is created using a background image with CSS positioning of team icons on top of that to represent cities. You can see how it looks here: WoA Map
The background image for the map is located here: Map background and created in Omnigraffle. It's not designed to draw maps but I'm hopelessly incompetent with photoshop and this works for my purposes just fine.
The problem comes that I want to perform such fun things as pathfinding and for that I need to have the map somehow stored in code. I have tried using PIL, I have looked at incorporating it with Blender, I tried going "old school" and creating tiles as from many older games and finally I tried to use SVG. I say this so you can see clearly that it's not through lack of trying that I have this problem ;)
I want to be able to store the map layout in code and both create an image from it and use it for things such as pathfinding. I'm using Python but I suspect that most answers will be generic. The cities other such things are stored already and easily drawn on, I want to store the layout of the landmass and features on the landmass.
As for pathfinding, each type of terrain has a movement cost and when the map is stored as just an image I can't access the terrain of a given area. In addition to pathfinding I wish to be able to know the terrain for various things related to the game, cities in mountains produce stone for example.
Is there a good way to do this and what terms should I have used in Google because the terms I tried all came up with unrelated stuff (mapping being something completely different most of the time).
Edit 2:
Armies can be placed anywhere on the map as can cities, well, anywhere but in the water where they'd sink, drown and probably complain (in that order).
After chatting to somebody on MSN who made me go over the really minute details and who has a better understanding of the game (owing to the fact that he's played it) it's occurring to me that tiles are the way to go but not the way I had initially thought. I put the bitmap down as it is now but also have a data layer of tiles, each tile has a given terrain type and thus pathfinding and suchlike can be done on it yet at the same time I still render using Omnigraffle which works pretty well.
I will be making an editor for this as suggested by Adam Smith. I don't know that graphs will be relevant Xynth but I've not had a chance to look into them fully yet.
I really appreciate all those that answered my question, thanks.
I'd store a game map in code as a graph.
Each node would represent a country/city and each edge would represent adjacency. Once you have a map like that, I'm sure you can find many resources on AI (pathfinding, strategy, etc.) online.
If you want to be able to build an image of the map programattically, consider adding an (x, y) coordinate and an image for each node. That way you can display all of the images at the given coordinates to build up a map view.
The key thing to realize here is that you don't have to use just one map. You can use two maps:
The one you already have which is drawn on screen
A hidden map which isn't drawn but which is used for path finding, collision detection etc.
The natural next question then is where does this second map come from? Easy, you create your own tool which can load your first map, and display it. Your tool will then let you draw boundaries around you islands and place markers at your cities. These markers and boundaries (simple polygons e.g.) are stored as your second map and is used in your code to do path finding etc.
In fact you can have your tool emit python code which creates the graphs and polygons so that you don't have to load any data yourself.
I am just basically telling you to make a level editor. It isn't very hard to do. You just need some buttons to click on to define what you are adding. e.g. if you are adding a polygon. Then you can just add each mouse coordinate to an array each time you click on your mouse if you have toggled your add polygon button. You can have another button for adding cities so that each time you click on the map you will record that coordinate for the city and possibly a corresponding name that you can provide in a text box.
You're going to have to translate your map into an abstract representation of some kind. Either a grid (hex or square) or a graph as xynth suggests. That's the only way you're going to be able to apply things like pathfinding algorithms to it.
IMO, the map should be rendered in the first place instead of being a bitmap. What you should be doing is to have separate objects each knowing its dimensions clearly such as a generic Area class and classes like City, Town etc derived from this class. Your objects should have all the information about their location, their terrain etc and should be rendered/painted etc. This way you will have exact knowledge of where everything lies.
Another option is to keep the bitmap as it is and keep this information in your objects as their data. By doing this the objects won't have a draw function but they will have precise information of their placement etc. This is sort of duplicating the data but if you want to go with the bitmap option, I can't think of any other way.
If you just want to do e.g. 2D hit-testing on the map, then storing it yourself is fine. There are a few possibilities for how you can store the information:
A polygon per island
Representing each island as union of a list rectangles (commonly used by windowing systems)
Creating a special (maybe greyscale) bitmap of the map which uses a unique solid colour for each island
Something more complex (perhaps whatever Omnigiraffe's internal representation is)
Asuming the map is fixed (not created on the fly) its "correct" to use a bitmap as graphical representation - you want to make it as pretty as possible.
For any game related features such as pathfinding or whatever fancy stuff you want to add you should add adequate data structures, even if that means some data is redundant.
E.g. describe the boundaries of the isles as polygon splines (either manually or automatically created from the bitmap, thats up to you and how much effort you want to spend and is needed to get the functionality you want).
To sum it up: create data structures matching the problems you have to solve, the bitmap is fine for looks but avoid doing pathfining or other stuff on it.

best way to print data in columnar format?

I am using Python to read in data in a user-unfriendly format and transform it into an easier-to-read format. The records I am outputting are usually going to be just a last name, first name, and room code. I
I would like to output a series of pages, each containing a contiguous subset of the total records, divided into multiple columns, each of which contains a contiguous subset of the total records on the page. (So in other words, you'd read down the first column, move to the next column, move to the next column, etc., and then start over on the next page...)
The problem I am facing now is that for output formats, I'm almost certainly limited to HTML (and Javascript, CSS, etc.) What is the best way to get the data into this columnar format? If I knew for certain that the printable area of the paper would hold 20 records vertically and five horizontally, for instance, I could easily print tables of 5x20, but I don't know if there's a way to indicate a page break -- and I don't know if there's any way to calculate programmatically how many records will fit on the page.
How would you approach this?
EDIT: The reason I said that I was limited in output: I have to produce the file on one computer, then bring it to a different computer upon which we cannot install new software and on which the selection of existing software is not optimal. The file itself is only going to be used to make a physical printout (which is what the end users will actually work with), but my time on the computer that I can print from is going to be limited, so I need to have the file all ready to go and print right away without a lot of tweaking.
Right now I've managed to find a word processor that I can use on the target machine, so I'm going to see if I can target a format that the word processor uses.
EDIT: Once I knew there was a word processor I could use, I made a simple skeleton file with the settings that I wanted (column and tab settings, monospaced font in a small point size, etc.) and then measured how many characters I got per line of a column and how many lines I got per column. I've watched the runs pretty carefully to make sure that there weren't some strange lines that somehow overflowed the characters-per-line guideline (which shouldn't happen with monospaced font, of course, but how many times do you end up having to figure out why that thing that "shouldn't" happen is happening anyways?)
If there hadn't been a word processor on the target machine that I could use, I probably would have looked at PDF as an output format.
"If I knew for certain that the printable area of the paper would hold 20 records vertically and five horizontally"
You do know that.
You know the size of your paper. You know the size of your font. You can easily do the math.
"almost certainly limited to HTML..." doesn't make much sense. Is this a web application? The page can have a "Previous" and "Next" button to step through the pages? Pick a size that looks good to you and display one page full with "Previous" and "Next" buttons.
If it's supposed to be one HTML page that prints correctly, that's hard. There are CSS things you can do, but you'll be happier creating a PDF file.
Get PyX or ReportLab and create a PDF that prints properly.
I -- personally -- have no patience with any of this. I try put this kind of thing into a CSV file. My users can then open CSV with a tool spreadsheet (Open Office Org has a good one) and then adjust the columns and print with it.

Categories