I'm keeping in memory huge list of companies, and need to do lots of operations of getting individual company.
Like getting individual company "Microsoft" by its symbol "MSFT".
Would the data structure below be a proper way to model that? There should be no copy-by-value of the whole list or map.
It is ok if the individual company would be copied by value.
import tables
type
Company = object
name: string
symbol: string
description: string
CompaniesRef = ref object
list: seq[Company]
map: Table[string, Company]
# Cached data structure to keep thousands of different companies
var cached_companies: CompaniesRef
proc companies(): CompaniesRef =
if cached_companies == nil:
# Here will be a proper code of loading companies into the
# CompaniesRef data structure
cached_companies = CompaniesRef()
cached_companies
# Lots of operations of getting a specific company from the list
# or from the map by its symbol
for i in 1..1000:
# it's ok if individual company will be copied by value,
# but the whole list should be passed by reference
let company1 = companies().list[0].name
# it's ok if individual company will be copied by value
# but the whole map should be passed by reference
let company2 = companies().map["MSFT"]
That global structure should be fine as it is, an object reference is just a memory managed pointer, so passing its reference around only copies the memory address. Unless you are going to do something with that pointer, why not create it as a global? Hiding it behind a proc call reeks of the I'm-afraid-of-globals-but-can't-live-without-them-singleton pattern.
let companies = CompaniesRef()
With regards to the contents of the structure, you are storing twice each Company object, you might want to store a reference to the Company in the Table or simply use an OrderedTable if you need to keep the order of the inserted keys.
Related
I have a list of valid values that I am storing in a data store. This list is about 20 items long now and will likely grow to around 100, maybe more.
I feel there are a variety of reasons it makes sense to store this in a data store rather than just storing in code. I want to be able to maintain the list and its metadata and make it accessible to other services, so it seems like a micro-service data store.
But in code, we want to make sure only values from the list are passed, and they can typically be hardcoded. So we would like to create an enum that can be used in code to ensure that valid values are passed.
I have created a simple node.js that can generate a JS file with the enum right from the data store. This could be regenerated anytime the file changes or maybe on a schedule. But sharing the enum file with any node.js applications that use it would not be trivial.
Has anyone done anything like this? Any reason why this would be a bad approach? Any feedback is welcome.
Piggy-backing off of this answer, which describes a way of creating an "enum" in JavaScript: you can grab the list of constants from your server (via an HTTP call) and then generate the enum in code, without the need for creating and loading a JavaScript source file.
Given that you have loaded your enumConstants from the back-end (here I hard-coded them):
const enumConstants = [
'FIRST',
'SECOND',
'THIRD'
];
const temp = {};
for (const constant of enumConstants) {
temp[constant] = constant;
}
const PlaceEnum = Object.freeze(temp);
console.log(PlaceEnum.FIRST);
// Or, in one line
const PlaceEnum2 = Object.freeze(enumConstants.reduce((o, c) => { o[c] = c; return o; }, {}));
console.log(PlaceEnum2.FIRST);
It is not ideal for code analysis or when using a smart editor, because the object is not explicitly defined and the editor will complain, but it will work.
Another approach is just to use an array and look for its members.
const members = ['first', 'second', 'third'...]
// then test for the members
members.indexOf('first') // 0
members.indexOf('third') // 2
members.indexOf('zero') // -1
members.indexOf('your_variable_to_test') // does it exist in the "enum"?
Any value that is >=0 will be a member of the list. -1 will not be a member. This doesn't "lock" the object like freeze (above) but I find it suffices for most of my similar scenarios.
Ok
i have this class in my model :
i want to get the agencys value which is a many to many on this class and store them in a list or array . Agency which store agency_id with the id of my class on a seprate table.
Agency has it's own tabel as well
class GPSpecial(BaseModel):
hotel = models.ForeignKey('Hotel')
rooms = models.ManyToManyField('Room')
agencys = models.ManyToManyField('Agency')
You can make it a bit more compact by using the flat=True parameter:
agencys_spe = list(GPSpecial.objects.values_list('agencys', flat=True))
The list(..) part is not necessary: without it, you have a QuerySet that contains the ids, and the query is postponed. By using list(..) we force the data into a list (and the query is executed).
It is possible that multiple GPSpecial objects have a common Agency, in that case it will be repeated. We can use the .distinct() function to prevent that:
agencys_spe = list(GPSpecial.objects.values_list('agencys', flat=True).distinct())
If you are however interested in the Agency objects, for example of GPSpecials that satisfy a certain predicate, you better query the Agency objects directly, like for example:
agencies = Agency.objects.filter(gpspecial__is_active=True).distinct()
will produce all Agency objects for which a GPSpecial object exists where is_active is set to True.
I think i found the answer to my question:
agencys_sp = GPSpecial.objects.filter(agencys=32,is_active=True).values_list('agencys')
agencys_spe = [i[0] for i in agencys_sp]
I have a treectrl structure which is populated from an external search of an open data set hosted by our municipal government. The data pertains to business licenses and is requested using Pandas and Sodapy. The tree is populated as follows:
for index, row in results_df.iterrows():
tradename = row['tradename']
address = row['address']
licTypes = row['licencetypes']
comm = row['comdistnm']
jobSts = row['jobstatusdesc']
jobCrt = row['jobcreated']
lng = row['longitude']
lng = str(lng)
lat = row['latitude']
lat = str(lat)
# Populate Tree Controls with DataFrame values
trdName = self.thrTree.AppendItem(root, tradename)
self.thrTree.AppendItem(trdName, address)
self.thrTree.AppendItem(trdName, licTypes)
self.thrTree.AppendItem(trdName, comm)
self.thrTree.AppendItem(trdName, jobSts)
self.thrTree.AppendItem(trdName, jobCrt)
self.thrTree.AppendItem(trdName, lng)
self.thrTree.AppendItem(trdName, lat)
This will result in a final structure of root, then node 1 with business name, and when expanded, contains all the information listed above, so I'm assuming root level, then child node 1, then child.child of node 1? Not even sure how the second second indented nodes are called. (I've heard the term leaf for the third level used before) But I digress; what I am interested in is grabbing the Latitude and Longitude of where the business is located, then allowing the user to map the location if they choose. I bind a wx.EVT_TREE_ITEM_ACTIVATED so that when the user double clicks on a business name to get the details, I want to grab the items displayed. This is how I am currently trying to iterate through the child nodes.
item = self.thrTree.GetSelection()
while self.thrTree.GetItemParent(item):
piece = self.thrTree.GetItemText(item)
tmpHldr.insert(0, piece)
item = self.thrTree.GetItemParent(item)
Looking at item, it appears to be collecting all the business names under root, and ignoring the third level items of interest.
What do I need to do to go deeper within the tree to grab the details under the business clicked on, and not just the list of business names under the root item, which is called 'Search Results'?
Thanks!
#YYC_Code,
Did you look here?
This has GetFirstChild()/GetNextChild() pair functions that you can use to iterate. It also has ItemHasChildren() function which you can use to verify if the item has any children and use the pair mentioned above if it does.
EDIT:
[quote]
For this enumeration function you must pass in a ‘cookie’ parameter which is opaque for the application but is necessary for the library to make these functions reentrant (i.e. allow more than one enumeration on one and the same object simultaneously). The cookie passed to GetFirstChild and GetNextChild should be the same variable.
[/quote]
You need to make sure that the cookie parameter is the same during the iteration.
You should also do this:
[quote]
Returns an invalid tree item (i.e. wx.TreeItemId.IsOk returns False) if there are no further children.
[/quote]
I have started using NSIncrementalStore. When you process a fetch request, you first have to process the predicate to get your internal reference objects. Then you convert these to objectID's and then you ask the context to get the corresponding managedObjects. At least that is my interpretation of the available documentation.
let fetchedReferences : [Int] = Array(names.keys) //names represent my backingstore
var fetchedObjectIDs : [NSManagedObjectID] = []
for reference in fetchedReferences
{
fetchedObjectIDs.append(self.newObjectIDForEntity(request.entity, referenceObject: reference))
}
var fetchedObjects : [NSManagedObject] = []
for objectID in fetchedObjectIDs
{
fetchedObjects.append(context.objectWithID(objectID))
}
"newObjectIDForEntity" is also used to obtain permanent objectID's (see obtainPermanentIDsForObjects)
I want to know what "newObjectIDForEntity" does. Does it make a new instance for the same object or does it each time internally create a new object? What I mean is this: if I create a new managed object and then fetch the object, I will have called "newObjectIDForEntity" twice for the same object. Does core data now think there are 1 or 2 objects?
Does it make a new instance for the same object or does it each time internally create a new object?
newObjectIDForEntity:referenceObject: is one of two utility methods for mapping between the store's internal representation of a managed object snapshot and an NSManagedObjectID. It's inverse is referenceObjectForObjectID:. As you might guess from the name, newObjectIDForEntity:referenceObject: returns an object considered to have a retain count of 1. newObjectIDForEntity:referenceObject: calls an internal factory method for generating an NSManagedObjectID that is unique to that reference object in this persistent store. Once that has been done, referenceObjectForObjectID: can look up that NSManagedObjectID and return the reference object it represents.
What I mean is this: if I create a new managed object and then fetch the object, I will have called "newObjectIDForEntity" twice for the same object. Does core data now think there are 1 or 2 objects?
I assume you mean an NSManagedObjectContext that is using your store creates the managed object. You can call newObjectIDForEntity:referenceObject: as many times as you want, the NSManagedObjectID instance may be different, but the data it represents is unchanged. It will know that it points to the same reference object as an earlier call with the same reference data and entity description.
I'm working on a project in Lua where I will be creating tables and storing them in a master table, to be erased at a later time. I will pass around references to these tables to other sibling tables.
master = {}
table.insert(master, {name = 'hello'})
table.insert(master, {name = 'world', pre = master[1]})
The problem that occurs is that when I wish to erase the reference from the master table, the reference still remains in master[2] here. Obviously my first solution was to make the tables have weak values. (through .__mode on a metatable, not shown here)
This worked, and would work, so long as I would never store a singly-referenced table within these tables.
table.insert(master, {name = 'goodbye', pre = master[2], some_table = {123}})
The third element, some_table would eventually be collected, because the tables have weak values, and this table (some_table) is not referenced anywhere else. This is undesired behavior. My latest solution involves creating "weak reference objects" to the tables within the master table. A naive implementation follows:
function WeakRef(t)
r = {__mode = 'v', __index = t, __newindex = t}
setmetatable(r, r)
return r
end
These weak reference objects act similarly to boost::weak_ptrs and accomplish my goal, but I am uncertain if they are the best solution to my problem.
Is there a better way; a more elegant solution?
Is my design, which requires this master table, perhaps flawed?
Given that:
You want master to be the "one place" where you define whether an object exists or not
Your objects can have links between them
Then probably the simplest architecture is reserving one of the members of each object as a "middle man" in charge of managing the references to others. Here're the steps:
Make master a regular table (not weak)
On each physical object, create a weak table called links (or whatever name suits your logic better)
Make all links tables weak. Use them to store references to other objects.
And this is a possible implementation. I've tried it in Lua 5.1:
local function newWeakTable()
return setmetatable({}, {__mode = "v"})
end
local master = {}
-- create two physical objects
local obj1 = { name = "obj1", links = newWeakTable() }
local obj2 = { name = "obj2", links = newWeakTable() }
-- link them
obj2.links.pre = obj1
-- insert them into master
table.insert(master, obj1)
table.insert(master, obj2)
-- master has 2 objects, and they are linked
assert(#master == 2)
assert(obj2.links.pre == obj1)
-- remove obj1 from master, and remove the variable reference
table.remove(master, 1)
obj1 = nil
-- run gc manually
collectgarbage("collect")
-- master has only 1 object now, and the link has dissapeared
assert(#master == 1)
assert(obj2.links.pre == nil)
print("Everything went as expected")