def remove_extra_spec(self, extra_spec_key_name): deleted_flag = False assert isinstance(extra_spec_key_name, basestring) try: LOG.debug("remove extra_spec {} from flavor {}".format( extra_spec_key_name, str(self))) for extra_spec in reversed(self.flavor_extra_specs): if extra_spec.key_name == extra_spec_key_name: self.flavor_extra_specs.remove(extra_spec) deleted_flag = True if not deleted_flag: # no need to raise in delete not found LOG.debug("Failed to remove extra_spec {0} from flavor id {1}". format(extra_spec_key_name, str(self.id))) except ErrorStatus as e: raise except Exception as exception: LOG.log_exception( "Failed to remove extra_spec {0} from flavor {1}".format( extra_spec_key_name, str(self)), exception) raise
def remove_tag(self, tag_name): deleted_flag = False assert isinstance(tag_name, basestring) try: LOG.debug("remove tag {0} from flavor {1}".format( tag_name, str(self))) for tag in reversed(self.flavor_tags): if tag.key_name == tag_name: self.flavor_tags.remove(tag) deleted_flag = True if not deleted_flag: raise ErrorStatus( 404, "Failed to remove tag {0} from flavor id {1}".format( tag_name, str(self.id))) except ErrorStatus as e: raise except Exception as exception: LOG.log_exception( "Failed to remove tag {0} from flavor {1}".format( tag_name, str(self)), exception) raise
def remove_region(self, region_name): region_deleted_flag = False assert isinstance(region_name, basestring) try: LOG.debug("remove regions {0} from flavor {1}".format( region_name, str(self))) for region in reversed(self.flavor_regions): if region.region_name == region_name: self.flavor_regions.remove(region) region_deleted_flag = True if not region_deleted_flag: raise ErrorStatus( 404, "Failed to remove region {0} from flavor id {1}".format( region_name, str(self.id))) except ErrorStatus as e: raise except Exception as exception: LOG.log_exception( "Failed to remove region {0} from flavor {1}".format( region_name, str(self)), exception) raise
def reversed(sequence): """ Return a sequence containing the elements of the input in reverse order. >>> reversed([3, 0, 1, 2]) [2, 1, 0, 3] """ return list(__builtin__.reversed(sequence))
def reversed(seq): try: return __builtin__.reversed(seq) except: def rev(): i = len(seq) - 1 while i >= 0: yield seq[i] i -= 1 raise StopIteration() return rev()
def to_and_fro_flight(list1): for i in list1: rev=i.split(',') rev[-1]=rev[-1].strip() l=[] diff=" ".join(reversed(rev)) l.append(diff) same=" ".join(rev) l1=[] l1.append(same) if cmp(l,l1) == 1: for item in l: print item
def remove_tenant(self, tenant_id): assert isinstance(tenant_id, basestring) try: LOG.debug("remove tenants {0} from flavor {1}".format( tenant_id, str(self))) for tenant in reversed(self.flavor_tenants): if tenant.tenant_id == tenant_id: self.flavor_tenants.remove(tenant) except Exception as exception: LOG.log_exception( "Failed to remove tenant {0} from flavor {1}".format( tenant_id, str(self)), exception) raise
def reversed(x): try: return __builtin__.reversed(x) except TypeError: return reversed(list(x))
def church_list(xs): churched = nil for x in reversed(list(xs)): churched = cons(x)(churched) return churched
def main(): # Remove file name from args args = (list)((reversed)((list)(takewhile(lambda x: file_no_ext(x) != file_no_ext(__file__), reversed(sys.argv))))) # Remove boolean flags from args args = (parse_boolean_args)(args) # Group args in pairs, parse args and remove any options from the args paths_to_backup = (list)(filter(lambda x: x if not parse_arg(x) else None, groupsof(2, args))) # Flatten (the paths will still be grouped) paths_to_backup = [path for paths in paths_to_backup for path in paths] # Create the output directory if needed os.path.isdir(out_path) or os.mkdir(out_path) (debug)(*('paths_to_backup: ' + repr(paths_to_backup), 2)) (debug)(*('output: {_coconut_format_0}'.format(_coconut_format_0=(out_path)), 2)) (debug)(*('force: {_coconut_format_0}'.format(_coconut_format_0=(cron_force)), 2)) (debug)(*('cron: {_coconut_format_0}'.format(_coconut_format_0=(cron_slices_str)), 2)) for path in paths_to_backup: if not exists(path): (error)("{_coconut_format_0} does not exist!".format(_coconut_format_0=(path))) continue if os.path.isdir(path): out_file_name = ((list)(dropwhile(lambda x: len(x) == 0, (reversed)(path.split("/")))))[0] else: out_file_name = basename(path) rsync_args = ["rsync", "-avz", normpath(path), normpath("{_coconut_format_0}/{_coconut_format_1}".format(_coconut_format_0=(out_path), _coconut_format_1=(out_file_name)))] (debug)("EXEC CMD: {_coconut_format_0}".format(_coconut_format_0=(' '.join(rsync_args)))) (debug)(subprocess.check_output(rsync_args, stderr=subprocess.STDOUT).decode('utf-8')) if cron_slices_str: user_cron = CronTab(user=True) cmd = " ".join(rsync_args) (debug)(*("Existing cron jobs: {_coconut_format_0}".format(_coconut_format_0=(repr(user_cron.crons))), 2)) # If task already exists try: job = _coconut_igetitem((_coconut.itertools.chain.from_iterable((_coconut_func() for _coconut_func in (lambda: user_cron.find_comment(fingerprint(cmd)), lambda: user_cron.find_command(cmd))))), 0) if job: if cron_force: (debug)("Cron job already exists... DELETING {_coconut_format_0})".format(_coconut_format_0=(job.comment))) user_cron.remove(job) else: (error)("Cron job already exists for {_coconut_format_0}!".format(_coconut_format_0=(cmd))) continue except StopIteration: pass job = (user_cron.new)(**{'command': cmd, 'comment': fingerprint(cmd)}) job.setall(cron_slices_str) if not job.is_valid: (error)("Cannot create cron job!: {_coconut_format_0}".format(_coconut_format_0=(repr(job)))) else: (debug)("Creating cron job: {_coconut_format_0}".format(_coconut_format_0=(job))) user_cron.write()
# add ~/.python/* to python module search path map(sys.path.append,__import__('glob').iglob(os.path.join(user.home.replace('\\',os.sep).replace('/',os.sep),'.python','*'))) ## include some functional primitives in the default namespace # box any specified arguments box = lambda *a: a # return a closure that executes ``f`` with the arguments unboxed. unbox = lambda f, *a, **k: lambda *ap, **kp: f(*(a + __builtin__.reduce(operator.add, __builtin__.map(__builtin__.tuple,ap), ())), **__builtin__.dict(k.items() + kp.items())) # return a closure that always returns ``n``. identity = lambda n: lambda *a, **k: n # return the first, second, or third item of a box. first, second, third = operator.itemgetter(0), operator.itemgetter(1), operator.itemgetter(2) # return a closure that executes a list of functions one after another from left-to-right fcompose = compose = lambda *f: __builtin__.reduce(lambda f1,f2: lambda *a: f1(f2(*a)), __builtin__.reversed(f)) # return a closure that executes function ``f`` whilst discarding any extra arguments fdiscard = lambda f: lambda *a, **k: f() # return a closure that executes function ``crit`` and then executes ``f`` or ``t`` based on whether or not it's successful. fcondition = lambda f, t: lambda crit: lambda *a, **k: t(*a, **k) if crit(*a, **k) else f(*a, **k) # return a closure that takes a list of functions to execute with the provided arguments fmaplist = fap = lambda *fa: lambda *a, **k: (f(*a, **k) for f in fa) #lazy = lambda f, state={}: lambda *a, **k: state[(f,a,__builtin__.tuple(__builtin__.sorted(k.items())))] if (f,a,__builtin__.tuple(__builtin__.sorted(k.items()))) in state else state.setdefault((f,a,__builtin__.tuple(__builtin__.sorted(k.items()))), f(*a, **k)) #lazy = lambda f, *a, **k: lambda *ap, **kp: f(*(a+ap), **dict(k.items() + kp.items())) # return a memoized closure that's lazy and only executes when evaluated def lazy(f, *a, **k): sortedtuple, state = fcompose(__builtin__.sorted, __builtin__.tuple), {} def closure(*ap, **kp): A, K = a+ap, sortedtuple(k.items() + kp.items()) return state[(A,K)] if (A,K) in state else state.setdefault((A,K), f(*A, **__builtin__.dict(k.items()+kp.items()))) return closure
# return a closure that will get a particular element from an object fgetitem = fitem = lambda item, *default: lambda object: default[0] if default and item not in object else object[item] # return a closure that will check if its argument has an ``attribute``. fhasattr = fattributeQ = lambda attribute: frpartial(builtins.hasattr, attribute) # return a closure that will get a particular attribute from an object fgetattr = fattribute = lambda attribute, *default: lambda object: getattr(object, attribute, *default) # return a closure that always returns ``object``. fconstant = fconst = falways = lambda object: lambda *a, **k: object # a closure that returns it's argument always fpassthru = fpass = fidentity = fid = lambda object: object # a closure that returns a default value if its object is false-y fdefault = lambda default: lambda object: object or default # return the first, second, or third item of a box. first, second, third, last = operator.itemgetter(0), operator.itemgetter(1), operator.itemgetter(2), operator.itemgetter(-1) # return a closure that executes a list of functions one after another from left-to-right fcompose = lambda *f: builtins.reduce(lambda f1, f2: lambda *a: f1(f2(*a)), builtins.reversed(f)) # return a closure that executes function ``f`` whilst discarding any extra arguments fdiscard = lambda f: lambda *a, **k: f() # return a closure that executes function ``crit`` and then returns/executes ``f`` or ``t`` based on whether or not it's successful. fcondition = fcond = lambda crit: lambda t, f: \ lambda *a, **k: (t(*a, **k) if builtins.callable(t) else t) if crit(*a, **k) else (f(*a, **k) if builtins.callable(f) else f) # return a closure that takes a list of functions to execute with the provided arguments fmap = lambda *fa: lambda *a, **k: (f(*a, **k) for f in fa) #lazy = lambda f, state={}: lambda *a, **k: state[(f, a, builtins.tuple(builtins.sorted(k.items())))] if (f, a, builtins.tuple(builtins.sorted(k.items()))) in state else state.setdefault((f, a, builtins.tuple(builtins.sorted(k.items()))), f(*a, **k)) #lazy = lambda f, *a, **k: lambda *ap, **kp: f(*(a+ap), **dict(k.items() + kp.items())) # return a memoized closure that's lazy and only executes when evaluated def flazy(f, *a, **k): sortedtuple, state = fcompose(builtins.sorted, builtins.tuple), {} def lazy(*ap, **kp): A, K = a+ap, sortedtuple(k.items() + kp.items()) return state[(A, K)] if (A, K) in state else state.setdefault((A, K), f(*A, **builtins.dict(k.items()+kp.items())))
### functional programming primitives (FIXME: probably better to document these with examples) # box any specified arguments fbox = fboxed = box = boxed = lambda *a: a # return a closure that executes ``f`` with the arguments unboxed. funbox = unbox = lambda f, *a, **k: lambda *ap, **kp: f(*(a + __builtin__.reduce(operator.add, __builtin__.map(__builtin__.tuple,ap), ())), **__builtin__.dict(k.items() + kp.items())) # return a closure that will check that ``object`` is an instance of ``type``. finstance = lambda type: lambda object: isinstance(object, type) # return a closure that always returns ``object``. fconstant = fconst = lambda object: lambda *a, **k: object # a closure that returns it's argument fpassthru = fpass = fidentity = fid = lambda object: object # return the first, second, or third item of a box. first, second, third, last = operator.itemgetter(0), operator.itemgetter(1), operator.itemgetter(2), operator.itemgetter(-1) # return a closure that executes a list of functions one after another from left-to-right fcompose = compose = lambda *f: __builtin__.reduce(lambda f1,f2: lambda *a: f1(f2(*a)), __builtin__.reversed(f)) # return a closure that executes function ``f`` whilst discarding any extra arguments fdiscard = lambda f: lambda *a, **k: f() # return a closure that executes function ``crit`` and then executes ``f`` or ``t`` based on whether or not it's successful. fcondition = fcond = lambda crit: lambda t, f: \ lambda *a, **k: t(*a, **k) if crit(*a, **k) else f(*a, **k) # return a closure that takes a list of functions to execute with the provided arguments fmaplist = fap = lambda *fa: lambda *a, **k: (f(*a, **k) for f in fa) #lazy = lambda f, state={}: lambda *a, **k: state[(f,a,__builtin__.tuple(__builtin__.sorted(k.items())))] if (f,a,__builtin__.tuple(__builtin__.sorted(k.items()))) in state else state.setdefault((f,a,__builtin__.tuple(__builtin__.sorted(k.items()))), f(*a, **k)) #lazy = lambda f, *a, **k: lambda *ap, **kp: f(*(a+ap), **dict(k.items() + kp.items())) # return a memoized closure that's lazy and only executes when evaluated def flazy(f, *a, **k): sortedtuple, state = fcompose(__builtin__.sorted, __builtin__.tuple), {} def lazy(*ap, **kp): A, K = a+ap, sortedtuple(k.items() + kp.items()) return state[(A,K)] if (A,K) in state else state.setdefault((A,K), f(*A, **__builtin__.dict(k.items()+kp.items())))