def __init__(self, tag, value, type, parent): self.parent = parent self.endian = parent.endian self.id = self.endian == "II" and util.reverse(tag) or tag self.type = type self.bytes = self.typeMap[self.type] self.setValue(value)
def parse_ifd(self, ifdOffset, context, next=None): """ Construct a modifiable Exif. Get nr of tags and parse each tag. If a pointer tags to other ifd is found, recurse inside that ifd as well """ ifd = self.tiff[ifdOffset:] nrTags = util.getNr(ifd[:2], self.endian) if DEBUG: print self.ifdName(context), "nr tags", nrTags offset = 2 #position after nr tags bytes for i in range(nrTags): try: tag = Tag(ifd[offset : offset+12], self) except Exception, e: #mainly unsuported tag types, etc # TODO: if a tag is buggy is it ok to just ignore it and go on or should we crash alltoghether if DEBUG: print "Can't create tag", str(e) offset += 12 continue context.append(tag) if next is not None: # and tag.value is not None: for ix in range(len(next)): nxtId = next[ix][0] if (tag.id == nxtId) or (self.endian=="II" and tag.id == util.reverse(nxtId)): next[ix][1] = tag.value offset += 12
def isLychrelNumber(n): count = 0 while(count < 50): n = n + reverse(n) if isPalindromic(n): return False count += 1 return True
def niceID(self): "return tag hex value and also a description if available" id = exif_tags_description.get(self.endian == "MM" and self.id or util.reverse(self.id), None) if id == None: id = hex(util.getNr(self.id, self.endian)) else: id = hex(util.getNr(self.id, self.endian)) + " - " + (type(id) == type(()) and id[0] or id) return id
def dict(self): "return a dictionary {tag description: (ifd, tag id, value)} of all parsed tags" d = {} for ifd in self.ifds: for tag in ifd: id = exif_tags_description.get(self.endian == "MM" and tag.id or util.reverse(tag.id), repr(tag.id)) id = type(id) == type(()) and id[0] or id d[id] = (self.ifdName(ifd), hex(util.getNr(tag.id, self.endian)), tag.value) return d
def getValue(self, parse=True): "return tag value, if posible parsed to a nice representation" if not parse: return self.value tag = self.id if self.endian == "II": tag = util.reverse(tag) val = exif_tags_description.get(tag, None) if val is None: return self.value if type(val) == type(()): return val[1].get(self.value, self.value) return self.value
def load_model(device): word_map = torchfile.load(os.path.join(PARAM_DIR, 'word_map.th7')) word_map = [w.decode('utf-8') for w in word_map] word_to_idx = {w: i for i, w in enumerate(word_map)} word_freq = torchfile.load(os.path.join(os.path.join(PARAM_DIR, 'word_freq.th7'))) mapto = torch.from_numpy(util.reverse(np.argsort(-word_freq))).long().to(device) with open(os.path.join(PARAM_DIR, 'lm.pt'), 'rb') as model_file: model = torch.load(model_file) model.full = True # Use real softmax--important! model.to(device) model.eval() return QueryHandler(model, word_to_idx, mapto, device)
def set(self, tag, value, targetIfd, type_): """ - Overwrite <tagID> tag with <value>. - If tag does not already exist, build it in the specified <targetIfd>. - The type (short, long, ascii, etc. as 3,4,2 etc.) need to be specified since this program will probably never be aware of all posible tags and their specification. You may need to do some research with exif.org to find the right type """ tagID = type(tag)==type(1) and util.setNr(tag)[2:] or tag #nr to str for ifd in self.ifds: for tag in ifd: if tag.id == tagID or (self.endian == "II" and tag.id == util.reverse(tagID)): tag.setValue(value) return True tag = FirstTag(tagID, value, type_, self) #don't have it yet, so create one targetIfd.append(tag)
torch.cuda.set_device(args.gpu) torch.cuda.manual_seed(args.seed) if not args.lm1b: with doing('Loading data'): corpus = corpus.Corpus(args.data, args.dic) ntokens = len(corpus.dictionary.idx2word) cutoffs = args.cutoffs + [ntokens] else: ############################################################################### # Load data ############################################################################### # Torch word_freq = load_lua(os.path.join(args.data, 'word_freq.th7')).numpy() mapto = torch.from_numpy(util.reverse(np.argsort(-word_freq))).long() print("load word frequency mapping - complete") ntokens = len(word_freq) nsampled = 8192 train_corpus = FastGBWDataset(args.data, 'train_data.th7', 'train_data.sid', mapto) print("load train data - complete") test_corpus = GBWDataset(args.data, 'test_data.th7', mapto) print("load test data - complete") cutoffs = args.cutoffs + [ntokens] # with doing('Constructing model'):
from util import reverse names = ['jesse', 'mike', 'joe', 'marco'] print "Names:" print names x = reverse(names) print "" print "Names Reversed: " print x x = sorted(x) print "" print "Sorted:" print x x = reverse(x) print "" print "Sorted Descending:" print x #sort in place x.sort(); print "" print "x.sort()" print x x = sorted(x, reverse=True) print "" print "Sorted in reverse"
def test_reverse_nonempty_list(self): self.assertListEqual(util.reverse(['blah', 'blerg']), ['blerg', 'blah'])
def test_reverse_none(self): with self.assertRaises(TypeError): util.reverse(None)
def test_reverse_empty_list(self): self.assertListEqual(util.reverse([]), [])
from util import (reverse, x) y = reverse('fernando') print y myX = x(1,2) print myX.y print myX.z