python - Search for a field in a compound object -



python - Search for a field in a compound object -

i'm new python, , creating programme tracks rfid badges used students in school district. in programme have class named badge creates badge object 6 public named variables (rfid, studentid, status, dateactivated, datedeactivated, reason). have class named badgetable holds badges.

what want see if specific rfid or studentid exists in of badges. know can loops, can take while (i'm reading in 450 csv files, each of contains 2000 14000 badges, , adding badge badgetable if hasn't been added).

edit: expand on above, badge info lastly 3 years has been manually entered spreadsheet has logic identify duplicate rfid or studentid numbers, exported each day csv file badge reader software. first year of our bus badge programme each day's csv file contained active badges; naively assumed if stopped sending inactive badges rejected badge reader. when discovered wrong, started including inactive badges knew in csv files (we added suffix studentid each inactive badge maintain them unique), on each days's csv file superset of previous day's file. however, there not insignificant number inactive/lost badges not in current csv file, above process meant way utilize old csv files a) finish list possible of badges we've issued, , b) eliminate misspellings in pupil names associated each badge.

i'd able like:

studentbadges = badgetable() # # bunch of code load badges studentbadges # if rfid not in studentbadges[:].rfid: studentbadges.add_badge(rfid, studentid, 'a', filedate, none, none)

what want see if variable rfid matches rfid field in of badges in studentbadges. i've tried that, , studentbadges[:][1], both fail (both 'badgetable' object not subscriptable)

my current workaround store separate list in badgetable class named rfidlist, , when each badge gets added badgetable add together it's rfid value rfidlist. long never sort either rfidlist or list of badges can things like:

def delete_badge(self, rfid): """delete individual badge badgetable """ if rfid in self.rfidlist: idx = self.rfidlist.index(rfid) del self.rfidlist[idx] del self.badgetable[idx]

is there more elegant, more pythonic way test existence of value in compound object in python 3, 1 doesn't involve keeping parallel lists in class?

i'm not quite sure why need badgetable class rather having studentbadges collection of badges. additional hold data?

in case, makes sense @ collections python has offer beyond lists. seems care uniqueness of badges, not order. lists (similar "arrays" in other programming languages) order. sets , dictionaries ("associative arrays" or "hash maps" in other languages) unordered, help uniqueness:

sets contain each object @ once dictionaries contain each key object @ once. each key points value object. (several keys might point same value.)

so have 2 dictionaries, 1 mapping rfids badges , 1 mapping studentids badges. lookup in these much quicker in lists.

but improve might have badges in single set. trying add together badge sec time nothing. holding (mathematical) set of unique objects (python) set does.

but when 2 objects "same"? instances of user-defined classes badge hashable default, compare unequal except themselves. each badge freshly create read-in info considered different others, if share rfid or studentid. thus, override methods __hash__() , __eq__() in badge, badge compare equal iff rfids match.

nota bene: equality consistence

equality should transitive. (i.e. if a == b , b == c, a == c.) create badges compare equal if both, rfids and studientids match, should not create them equal if any, rfids or studientids match. (it'd impossible find meaningful __hash__() implementation consistent latter, anyway.)

though guess if pupil has been given several badges (with different rfids) in or school career programme might want know more first badge, anyway.

6'300'000 badges

looking @ amounts of info you're handling here, sure want (and can) hold in memory? if not, using python access info base of operations management scheme of selection way go. relational databases back upwards set operation semantics you'd want rely on.

python python-3.x

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -