prevent beeprint or prettyprint from ordering dict keys in alphabetical order - pretty-print

Hello everyone, here is my pb:
Since i'm using python 3.9, 'm aware that dictionnary keys are displayed in the order of their input.
Still, i'd like the resulting dict to be "pretty" displayed.
Below is the original dict with the correct key order:
{'intitule_semestre': 'adapt', 'semaine_debut': 38, 'annee_debut': 2020, 'semaine_fin': 44, 'annee_fin': 2020, 'nb_semaines': 7, 'plage_semestre': 2020-09-14T00:00:00 - 2020-10-30T00:00:00}
And here's the pprint result (using pprint or beeprint):
{
'annee_debut': 2020,
'annee_fin': 2020,
'intitule_semestre': 'adapt',
'nb_semaines': 7,
'plage_semestre': 2020-09-14T00:00:00 - 2020-10-30T00:00:00
'semaine_debut': 38,
'semaine_fin': 44,
}
=> alphabetical order!!! Why is that?
How can I pretty print my dict with keeping the original order?
Thank for your help
Zemaf

Finally figured out how to work this out : by setting PrettyPrinter parameter "sort_dicts" to False keys are ordered according to input order, not alphabetical order (sort_dicts=True by default)!

Related

subscript Array change coordinates

How can I change subscript coordinates in an Array of Arrays [[Int]] so that the way to address a cell would be table[column-letter][row-number], same as Excel, instead of current ios table[row-number][column-number] for a given table like:
var table = [[0, 1, 2, 3],
[1, 32, 44, 25],
[2, 12, 66, 43],
[3, 3, 4, 5]]
Today table[2][1] = 12, but I want table[C][1] = 44.
I need this change to adapt formulas from excel into my app without changing the coordinate system or the tables from an Excel.
I know there are subscript functions in Array class which may help, but I couldnĀ“t make it work so far.
Today table2 = 12, but I want tableC = 44.
Do you know what is [2][1] actually means you are telling the compiler,
i want the element in the array that has index of [2] and by adding another [1], you are telling it that you need the element that has index of [1] that its inside the result of [2].
its an Array of Arrays if you take another look at it empty would be something like this ,
[[],[],[]] // no matter what is the values inside indexing is always the same
with that pointed out now lets take a look on what are you trying to do.
[C][1] = 44
As you can now probably tell this is not valid because [C] is not a valid index as index accept only Integer Types.
So what can we try ?.
Well you can try dictionaries
Read about them here
With dictionary at some point you will be able to get a value like this
value = table["C"]?[1]
or take a look on this example.
var foo :[String:[Int]] = ["A":[1,2,3], "B":[1,2,3]]
let colm = foo["A"]?[0]
Edit another solution inspired by #Paulw11
using this "A".utf16.first! - 65 will return 0 index to use inside the Array so you can additionally say from A to Z
table = ["MyChar".utf16.first! - 65][2]

Understanding GameMonkey Script Mixed Arrays

I was just reading some introductory stuff from GameMonkey Script on https://www.gamedev.net/articles/programming/engines-and-middleware/introduction-to-gamemonkey-script-r3297/ and when they were explaining about Mixed Arrays they say that you can access the elements using and index or a key depending on how the value was declared, so for example if i have the next array
myMixedArray = table( 1, 3, 4, KeyV = "Test", 33);
then i can access 1, 2, 4 and 33 using the next indices 0, 1, 2, 3 and
to access "Test" i'll do it like this
myMixedArray["KeyV"] <- ("Test")
now according with the following image that you can find in the above link
The number expected to be at myTest[3] is 7, but that would mean that both regular values and key-val elements are not really separated in the array.
If not then why would 7 be at the index 3 of the array?
While you can treat a gm Table as an Array or Map, you can't effectively do both at the same time.
Internally, the Table is just a hash table, and your index access method is a bit like an iterator.
In your example, because value "Test" is assigned to key 'KeyV', it messes up the otherwise contiguous index order.
Hopefully that gives you an idea of the cause. Try iterating a table with no 'keys' and again with all key value pairs. Observe the different behavior.
If you are serious about arrays, you may be better off using a binding to create an Array type with the behavior you want. GM source has an example of an array container.

Prepend multiple data to beginning of Core Data (Swift)

Could someone give some guidance on how to prepend newly ingested data to Core Data in the correct index order?
I have Core Data with an attribute, contentID. The objects are listed as: 5, 4, 3, 2, 1, 0
I query a web database that pushes updates since the highest contentID, 5, and it returns an array with updates: 7, 6
If you simply append it to Core Data, the order will go: 5, 4, 3, 2, 1, 0, 7, 6
How do you prepend it to the front correctly?
And also how do you prepend it in the correct order so that when you prepend the first new object, 7, and then prepend the second item, 6, it doesn't go: 6, 7, 5, 4, 3, 2, 1, 0
Hope this makes sense... Thanks!
In general, you should not concern yourself with the order in which objects are written to or stored in the database. Instead, specify the sort order in which you wish to receive the data when you fetch it from the database.
The sort order for a fetch is determined by the sortDescriptors property of the NSFetchRequest. Each NSSortDescriptor specifies the key to determine the order, and whether the results should be in ascending order (v. descending).
So when you perform a fetch, you should in your case specify:
let sort = NSSortDescriptor(key:"contentID", ascending:false)
fetchRequest.sortDescriptors = [sort]
let fetchedResults = managedObjectContent.executeFetchRequest(fetchRequest, error: &error) as? [NSManagedObject]

Tag "metadata" onto an array in ruby

I have some code which assembles two-dimensional arrays to be sent to the Spreadsheet gem's excel-building methods. I'd like to "tag" some subarrays (corresponding to rows) with formatting codes like {:color => "red"}.
Can anyone see a way of doing this? I could achieve a similar result by storing a seperate object which has the formatting option and (for example) the indexes to all rows i want to apply that format to. But it would be nicer if I could stick it straight onto the row itself as i build my data.
One thing that occurred to me is to use some kind of namespaced hash as the last entry in an array, if i want to format it, and then strip that out again in the spreadsheet builder. But, this seems risky as it's then in the array's actual data. Is there any kind of instance variable or something i can tap into with an array to "shove" my metadata in there?
I'm using Rails 2.2 for this app, in case that's relevant.
Since each row contains data and possibly some metadata, it seems natural to represent all rows as an Array of Hashes, where each Hash contains a :data key and a :metadata key. The latter can be omitted or can just point to an empty Hash if you do not have any metadata for the row, whichever you prefer.
I have no experience with the Spreadsheet gem, but I assume it requires an Array of Arrays as input, which you can create in a straightforward manner from the Array of Hashes as shown in the code below.
rows = [
{
data: [2, 3, 5],
metadata: { color: 'red' }
},
{
data: [7, 11, 13],
metadata: {}
}
]
# Transform into Array of Arrays, removing all metadata
rows.map { |row| row[:data] }
# => [[2, 3, 5], [7, 11, 13]]

groovy simple way to find hole in list?

I'm using the grails findAllBy() method to return a list of Position(s). Position has an integer field called location, which ranges from 1 to 15. I need to find the lowest location in the position list that is free.
For example, if there are positions at locations 1,2 and 4, then the algorithm should return 3. If locations 1 - 4 were filled, it would return 5.
Is there some simple groovy list/map functions to get the right number?
Thanks
If your list of positions were (limited to a mx of 5 for brevity):
def list = [ 1, 2, 4, 5 ]
And you know that you have a maximum of 5 of them, you can do:
(1..5).minus(list).min()
Which would give you 3
Just another option, because I originally thought he wanted to know the first unused slot in a list, say you had this:
def list = ['a', 'b', null, 'd', 'e', null, 'g']
You could easily find the first empty slot in the array by doing this:
def firstOpen = list.findIndexOf{ !it } // or it == null if you need to avoid groovy truth
Tim's way works, and is good for small ranges. If you've got the items sorted by location already, you can do it in O(n) by leveraging findResult
def firstMissing = 0
println list.findResult { (it.location != ++firstMissing) ? firstMissing : null }
prints 3.
If they're not sorted, you can either modify your db query to sort them, or add sort{it.location} in there.

Resources