idx
int64
0
63k
question
stringlengths
61
4.03k
target
stringlengths
6
1.23k
47,600
def customers ( self ) : return Customer . objects . filter ( Q ( privatelessoncustomer__lesson = self ) | Q ( registration__eventregistration__event = self ) ) . distinct ( )
List both any individuals signed up via the registration and payment system and any individuals signed up without payment .
47,601
def save ( self , * args , ** kwargs ) : if not self . status : self . status == Event . RegStatus . hidden super ( PrivateLessonEvent , self ) . save ( * args , ** kwargs )
Set registration status to hidden if it is not specified otherwise
47,602
def availableDurations ( self ) : potential_slots = InstructorAvailabilitySlot . objects . filter ( instructor = self . instructor , location = self . location , room = self . room , pricingTier = self . pricingTier , startTime__gte = self . startTime , startTime__lte = self . startTime + timedelta ( minutes = getConst...
A lesson can always be booked for the length of a single slot but this method checks if multiple slots are available . This method requires that slots are non - overlapping which needs to be enforced on slot save .
47,603
def availableRoles ( self ) : if not hasattr ( self . instructor , 'instructorprivatelessondetails' ) : return [ ] return [ [ x . id , x . name ] for x in self . instructor . instructorprivatelessondetails . roles . all ( ) ]
Some instructors only offer private lessons for certain roles so we should only allow booking for the roles that have been selected for the instructor .
47,604
def checkIfAvailable ( self , dateTime = timezone . now ( ) ) : return ( self . startTime >= dateTime + timedelta ( days = getConstant ( 'privateLessons__closeBookingDays' ) ) and self . startTime <= dateTime + timedelta ( days = getConstant ( 'privateLessons__openBookingDays' ) ) and not self . eventRegistration and (...
Available slots are available but also tentative slots that have been held as tentative past their expiration date
47,605
def json_event_feed ( request , location_id = None , room_id = None ) : if not getConstant ( 'calendar__privateCalendarFeedEnabled' ) or not request . user . is_staff : return JsonResponse ( { } ) this_user = request . user startDate = request . GET . get ( 'start' , '' ) endDate = request . GET . get ( 'end' , '' ) ti...
The Jquery fullcalendar app requires a JSON news feed so this function creates the feed from upcoming PrivateEvent objects
47,606
def checkRequirements ( sender , ** kwargs ) : if not getConstant ( 'requirements__enableRequirements' ) : return logger . debug ( 'Signal to check RegistrationContactForm handled by prerequisites app.' ) formData = kwargs . get ( 'formData' , { } ) first = formData . get ( 'firstName' ) last = formData . get ( 'lastNa...
Check that the customer meets all prerequisites for the items in the registration .
47,607
def get_email_context ( self , ** kwargs ) : context = kwargs context . update ( { 'currencyCode' : getConstant ( 'general__currencyCode' ) , 'currencySymbol' : getConstant ( 'general__currencySymbol' ) , 'businessName' : getConstant ( 'contact__businessName' ) , 'site_url' : getConstant ( 'email__linkProtocol' ) + ':/...
This method can be overridden in classes that inherit from this mixin so that additional object - specific context is provided to the email template . This should return a dictionary . By default only general financial context variables are added to the dictionary and kwargs are just passed directly . Note also that it...
47,608
def get_group_required ( self ) : this_object = self . model_object if hasattr ( this_object , self . group_required_field ) : if hasattr ( getattr ( this_object , self . group_required_field ) , 'name' ) : return [ getattr ( this_object , self . group_required_field ) . name ] return [ '' ]
Get the group_required value from the object
47,609
def check_membership ( self , groups ) : if not groups or groups == [ '' ] : return True if self . request . user . is_superuser : return True user_groups = self . request . user . groups . values_list ( "name" , flat = True ) return set ( groups ) . intersection ( set ( user_groups ) )
Allows for objects with no required groups
47,610
def dispatch ( self , request , * args , ** kwargs ) : self . request = request in_group = False required_group = self . get_group_required ( ) if not required_group or required_group == [ '' ] : in_group = True elif self . request . user . is_authenticated ( ) : in_group = self . check_membership ( required_group ) if...
This override of dispatch ensures that if no group is required then the request still goes through without being logged in .
47,611
def validate ( self , value ) : super ( ChoiceField , self ) . validate ( value ) try : get_template ( value ) except TemplateDoesNotExist : raise ValidationError ( _ ( '%s is not a valid template.' % value ) )
Check for empty values and for an existing template but do not check if this is one of the initial choices provided .
47,612
def render ( self , context , instance , placeholder ) : if instance and instance . template : self . render_template = instance . template return super ( PluginTemplateMixin , self ) . render ( context , instance , placeholder )
Permits setting of the template in the plugin instance configuration
47,613
def get_return_page ( self , prior = False ) : siteHistory = self . request . session . get ( 'SITE_HISTORY' , { } ) return getReturnPage ( siteHistory , prior = prior )
This is just a wrapper for the getReturnPage helper function .
47,614
def is_valid ( self ) : valid = super ( RegistrationContactForm , self ) . is_valid ( ) msgs = messages . get_messages ( self . _request ) prior_messages = self . _session . pop ( 'prior_messages' , [ ] ) remaining_messages = [ ] for m in msgs : m_dict = { 'message' : m . message , 'level' : m . level , 'extra_tags' : ...
For this form to be considered valid there must be not only no errors but also no messages on the request that need to be shown .
47,615
def clean_total_refund_amount ( self ) : initial = self . cleaned_data . get ( 'initial_refund_amount' , 0 ) total = self . cleaned_data [ 'total_refund_amount' ] summed_refunds = sum ( [ v for k , v in self . cleaned_data . items ( ) if k . startswith ( 'item_refundamount_' ) ] ) if not self . cleaned_data . get ( 'id...
The Javascript should ensure that the hidden input is updated but double check it here .
47,616
def clean ( self ) : super ( SubstituteReportingForm , self ) . clean ( ) occurrences = self . cleaned_data . get ( 'occurrences' , [ ] ) staffMember = self . cleaned_data . get ( 'staffMember' ) replacementFor = self . cleaned_data . get ( 'replacedStaffMember' , [ ] ) event = self . cleaned_data . get ( 'event' ) for...
This code prevents multiple individuals from substituting for the same class and class teacher . It also prevents an individual from substituting for a class in which they are a teacher .
47,617
def save ( self , commit = True ) : existing_record = EventStaffMember . objects . filter ( staffMember = self . cleaned_data . get ( 'staffMember' ) , event = self . cleaned_data . get ( 'event' ) , category = getConstant ( 'general__eventStaffCategorySubstitute' ) , replacedStaffMember = self . cleaned_data . get ( '...
If a staff member is reporting substitute teaching for a second time then we should update the list of occurrences for which they are a substitute on their existing EventStaffMember record rather than creating a new record and creating database issues .
47,618
def save ( self , commit = True ) : if getattr ( self . instance , 'instructor' , None ) : self . instance . instructor . availableForPrivates = self . cleaned_data . pop ( 'availableForPrivates' , self . instance . instructor . availableForPrivates ) self . instance . instructor . save ( update_fields = [ 'availableFo...
If the staff member is an instructor also update the availableForPrivates field on the Instructor record .
47,619
def save ( self , * args , ** kwargs ) : if not self . pluralName : self . pluralName = self . name + 's' super ( self . __class__ , self ) . save ( * args , ** kwargs )
Just add s if no plural name given .
47,620
def getBasePrice ( self , ** kwargs ) : payAtDoor = kwargs . get ( 'payAtDoor' , False ) dropIns = kwargs . get ( 'dropIns' , 0 ) if dropIns : return dropIns * self . dropinPrice if payAtDoor : return self . doorPrice return self . onlinePrice
This handles the logic of finding the correct price . If more sophisticated discounting systems are needed then this PricingTier model can be subclassed or the discounts and vouchers apps can be used .
47,621
def availableRoles ( self ) : eventRoles = self . eventrole_set . filter ( capacity__gt = 0 ) if eventRoles . count ( ) > 0 : return [ x . role for x in eventRoles ] elif isinstance ( self , Series ) : return self . classDescription . danceTypeLevel . danceType . roles . all ( ) return [ ]
Returns the set of roles for this event . Since roles are not always custom specified for event this looks for the set of available roles in multiple places . If no roles are found then the method returns an empty list in which case it can be assumed that the event s registration is not role - specific .
47,622
def numRegisteredForRole ( self , role , includeTemporaryRegs = False ) : count = self . eventregistration_set . filter ( cancelled = False , dropIn = False , role = role ) . count ( ) if includeTemporaryRegs : count += self . temporaryeventregistration_set . filter ( dropIn = False , role = role ) . exclude ( registra...
Accepts a DanceRole object and returns the number of registrations of that role .
47,623
def capacityForRole ( self , role ) : if isinstance ( role , DanceRole ) : role_id = role . id else : role_id = role eventRoles = self . eventrole_set . filter ( capacity__gt = 0 ) if eventRoles . count ( ) > 0 and role_id not in [ x . role . id for x in eventRoles ] : return 0 elif eventRoles . count ( ) > 0 : return ...
Accepts a DanceRole object and determines the capacity for that role at this event . this Since roles are not always custom specified for events this looks for the set of available roles in multiple places and only returns the overall capacity of the event if roles are not found elsewhere .
47,624
def soldOutForRole ( self , role , includeTemporaryRegs = False ) : return self . numRegisteredForRole ( role , includeTemporaryRegs = includeTemporaryRegs ) >= ( self . capacityForRole ( role ) or 0 )
Accepts a DanceRole object and responds if the number of registrations for that role exceeds the capacity for that role at this event .
47,625
def allDayForDate ( self , this_date , timeZone = None ) : if isinstance ( this_date , datetime ) : d = this_date . date ( ) else : d = this_date date_start = datetime ( d . year , d . month , d . day ) naive_start = self . startTime if timezone . is_naive ( self . startTime ) else timezone . make_naive ( self . startT...
This method determines whether the occurrence lasts the entirety of a specified day in the specified time zone . If no time zone is specified then it uses the default time zone ) . Also give a grace period of a few minutes to account for issues with the way events are sometimes entered .
47,626
def netHours ( self ) : if self . specifiedHours is not None : return self . specifiedHours elif self . category in [ getConstant ( 'general__eventStaffCategoryAssistant' ) , getConstant ( 'general__eventStaffCategoryInstructor' ) ] : return self . event . duration - sum ( [ sub . netHours for sub in self . replacement...
For regular event staff this is the net hours worked for financial purposes . For Instructors netHours is caclulated net of any substitutes .
47,627
def shortDescription ( self ) : cd = getattr ( self , 'classDescription' , None ) if cd : sd = getattr ( cd , 'shortDescription' , '' ) d = getattr ( cd , 'description' , '' ) return sd if sd else d return ''
Overrides property from Event base class .
47,628
def netHours ( self ) : if self . specifiedHours is not None : return self . specifiedHours return self . event . duration - sum ( [ sub . netHours for sub in self . replacementFor . all ( ) ] )
For regular event staff this is the net hours worked for financial purposes . For Instructors netHours is calculated net of any substitutes .
47,629
def getTimeOfClassesRemaining ( self , numClasses = 0 ) : occurrences = EventOccurrence . objects . filter ( cancelled = False , event__in = [ x . event for x in self . temporaryeventregistration_set . filter ( event__series__isnull = False ) ] , ) . order_by ( '-endTime' ) if occurrences . count ( ) > numClasses : ret...
For checking things like prerequisites it s useful to check if a requirement is almost met
47,630
def finalize ( self , ** kwargs ) : dateTime = kwargs . pop ( 'dateTime' , timezone . now ( ) ) sendEmail = kwargs . pop ( 'sendEmail' , True ) customer , created = Customer . objects . update_or_create ( first_name = self . firstName , last_name = self . lastName , email = self . email , defaults = { 'phone' : self . ...
This method is called when the payment process has been completed and a registration is ready to be finalized . It also fires the post - registration signal
47,631
def save ( self , * args , ** kwargs ) : if self . send_html : self . content = get_text_for_html ( self . html_content ) else : self . html_content = None super ( EmailTemplate , self ) . save ( * args , ** kwargs )
If this is an HTML template then set the non - HTML content to be the stripped version of the HTML . If this is a plain text template then set the HTML content to be null .
47,632
def create_from_registration ( cls , reg , ** kwargs ) : submissionUser = kwargs . pop ( 'submissionUser' , None ) collectedByUser = kwargs . pop ( 'collectedByUser' , None ) status = kwargs . pop ( 'status' , Invoice . PaymentStatus . unpaid ) new_invoice = cls ( firstName = reg . firstName , lastName = reg . lastName...
Handles the creation of an Invoice as well as one InvoiceItem per assodciated TemporaryEventRegistration or registration . Also handles taxes appropriately .
47,633
def url ( self ) : if self . id : return '%s://%s%s' % ( getConstant ( 'email__linkProtocol' ) , Site . objects . get_current ( ) . domain , reverse ( 'viewInvoice' , args = [ self . id , ] ) , )
Because invoice URLs are generally emailed this includes the default site URL and the protocol specified in settings .
47,634
def calculateTaxes ( self ) : tax_rate = ( getConstant ( 'registration__salesTaxRate' ) or 0 ) / 100 if tax_rate > 0 : if self . buyerPaysSalesTax : self . taxes = self . total * tax_rate else : adjusted_total = self . total / ( 1 + tax_rate ) self . taxes = adjusted_total * tax_rate
Updates the tax field to reflect the amount of taxes depending on the local rate as well as whether the buyer or seller pays sales tax .
47,635
def allocateFees ( self ) : items = list ( self . invoiceitem_set . all ( ) ) if self . total != sum ( [ x . total for x in items ] ) : msg = _ ( 'Invoice item totals do not match invoice total. Unable to allocate fees.' ) logger . error ( str ( msg ) ) raise ValidationError ( msg ) if self . adjustments != sum ( [ x ...
Fees are allocated across invoice items based on their discounted total price net of adjustments as a proportion of the overall invoice s total price
47,636
def applyAndAllocate ( self , allocatedPrices , tieredTuples , payAtDoor = False ) : initial_net_price = sum ( [ x for x in allocatedPrices ] ) if self . discountType == self . DiscountType . flatPrice : applicable_price = self . getFlatPrice ( payAtDoor ) or 0 this_price = applicable_price + sum ( [ x [ 0 ] . event . ...
This method takes an initial allocation of prices across events and an identical length list of allocation tuples . It applies the rule specified by this discount allocates the discount across the listed items and returns both the price and the allocation
47,637
def getComponentList ( self ) : component_list = [ ] for x in self . discountcombocomponent_set . all ( ) : for y in range ( 0 , x . quantity ) : component_list += [ x ] component_list . sort ( key = lambda x : x . quantity , reverse = True ) return component_list
This function just returns a list with items that are supposed to be present in the the list multiple times as multiple elements of the list . It simplifies checking whether a discount s conditions are satisfied .
47,638
def save ( self , * args , ** kwargs ) : if self . discountType != self . DiscountType . flatPrice : self . onlinePrice = None self . doorPrice = None if self . discountType != self . DiscountType . dollarDiscount : self . dollarDiscount = None if self . discountType != self . DiscountType . percentDiscount : self . pe...
Don t save any passed values related to a type of discount that is not the specified type
47,639
def checkVoucherCode ( sender , ** kwargs ) : logger . debug ( 'Signal to check RegistrationContactForm handled by vouchers app.' ) formData = kwargs . get ( 'formData' , { } ) request = kwargs . get ( 'request' , { } ) registration = kwargs . get ( 'registration' , None ) session = getattr ( request , 'session' , { } ...
Check that the given voucher code is valid
47,640
def applyVoucherCodeTemporarily ( sender , ** kwargs ) : logger . debug ( 'Signal fired to apply temporary vouchers.' ) reg = kwargs . pop ( 'registration' ) voucherId = reg . data . get ( 'gift' , '' ) try : voucher = Voucher . objects . get ( voucherId = voucherId ) except ObjectDoesNotExist : logger . debug ( 'No ap...
When the core registration system creates a temporary registration with a voucher code the voucher app looks for vouchers that match that code and creates TemporaryVoucherUse objects to keep track of the fact that the voucher may be used .
47,641
def applyReferrerVouchersTemporarily ( sender , ** kwargs ) : if not getConstant ( 'referrals__enableReferralProgram' ) : return logger . debug ( 'Signal fired to temporarily apply referrer vouchers.' ) reg = kwargs . pop ( 'registration' ) try : c = Customer . objects . get ( user__email = reg . email ) vouchers = c ....
Unlike voucher codes which have to be manually supplied referrer discounts are automatically applied here assuming that the referral program is enabled .
47,642
def applyVoucherCodesFinal ( sender , ** kwargs ) : logger . debug ( 'Signal fired to mark voucher codes as applied.' ) finalReg = kwargs . pop ( 'registration' ) tr = finalReg . temporaryRegistration tvus = TemporaryVoucherUse . objects . filter ( registration = tr ) for tvu in tvus : vu = VoucherUse ( voucher = tvu ....
Once a registration has been completed vouchers are used and referrers are awarded
47,643
def provideCustomerReferralCode ( sender , ** kwargs ) : customer = kwargs . pop ( 'customer' ) if getConstant ( 'vouchers__enableVouchers' ) and getConstant ( 'referrals__enableReferralProgram' ) : vrd = ensureReferralVouchersExist ( customer ) return { 'referralVoucherId' : vrd . referreeVoucher . voucherId }
If the vouchers app is installed and referrals are enabled then the customer s profile page can show their voucher referral code .
47,644
def get_prep_lookup ( self ) : raise FieldError ( "{} '{}' does not support lookups" . format ( self . lhs . field . __class__ . __name__ , self . lookup_name ) )
Raise errors for unsupported lookups
47,645
def derive_fernet_key ( input_key ) : hkdf = HKDF ( algorithm = hashes . SHA256 ( ) , length = 32 , salt = salt , info = info , backend = backend , ) return base64 . urlsafe_b64encode ( hkdf . derive ( force_bytes ( input_key ) ) )
Derive a 32 - bit b64 - encoded Fernet key from arbitrary input key .
47,646
def reduce_cpu ( f , x , axes , dtype ) : axes = _get_axes ( axes , x . ndim ) if not axes : return x permute = [ n for n in range ( x . ndim ) if n not in axes ] permute = axes + permute T = x . transpose ( permute = permute ) N = len ( axes ) t = T . type . at ( N , dtype = dtype ) acc = x . empty ( t , device = x . ...
NumPy s reduce in terms of fold .
47,647
def reduce_cuda ( g , x , axes , dtype ) : if axes != 0 : raise NotImplementedError ( "'axes' keyword is not implemented for CUDA" ) return g ( x , dtype = dtype )
Reductions in CUDA use the thrust library for speed and have limited functionality .
47,648
def maxlevel ( lst ) : maxlev = 0 def f ( lst , level ) : nonlocal maxlev if isinstance ( lst , list ) : level += 1 maxlev = max ( level , maxlev ) for item in lst : f ( item , level ) f ( lst , 0 ) return maxlev
Return maximum nesting depth
47,649
def getitem ( lst , indices ) : if not indices : return lst i , indices = indices [ 0 ] , indices [ 1 : ] item = list . __getitem__ ( lst , i ) if isinstance ( i , int ) : return getitem ( item , indices ) if not item : if lst : _ = getitem ( lst , ( slice ( None ) , ) + indices ) elif any ( isinstance ( k , int ) for ...
Definition for multidimensional slicing and indexing on arbitrarily shaped nested lists .
47,650
def genslices ( n ) : def range_with_none ( ) : yield None yield from range ( - n , n + 1 ) for t in product ( range_with_none ( ) , range_with_none ( ) , range_with_none ( ) ) : s = slice ( * t ) if s . step != 0 : yield s
Generate all possible slices for a single dimension .
47,651
def genslices_ndim ( ndim , shape ) : iterables = [ genslices ( shape [ n ] ) for n in range ( ndim ) ] yield from product ( * iterables )
Generate all possible slice tuples for shape .
47,652
def mutator ( mutate ) : @ functools . wraps ( mutate ) def inspyred_mutator ( random , candidates , args ) : mutants = [ ] for i , cs in enumerate ( candidates ) : mutants . append ( mutate ( random , cs , args ) ) return mutants inspyred_mutator . single_mutation = mutate return inspyred_mutator
Return an inspyred mutator function based on the given function . This function generator takes a function that operates on only one candidate to produce a single mutated candidate . The generator handles the iteration over each candidate in the set to be mutated .
47,653
def bit_flip_mutation ( random , candidate , args ) : rate = args . setdefault ( 'mutation_rate' , 0.1 ) mutant = copy . copy ( candidate ) if len ( mutant ) == len ( [ x for x in mutant if x in [ 0 , 1 ] ] ) : for i , m in enumerate ( mutant ) : if random . random ( ) < rate : mutant [ i ] = ( m + 1 ) % 2 return mutan...
Return the mutants produced by bit - flip mutation on the candidates .
47,654
def random_reset_mutation ( random , candidate , args ) : bounder = args [ '_ec' ] . bounder try : values = bounder . values except AttributeError : values = None if values is not None : rate = args . setdefault ( 'mutation_rate' , 0.1 ) mutant = copy . copy ( candidate ) for i , m in enumerate ( mutant ) : if random ....
Return the mutants produced by randomly choosing new values .
47,655
def scramble_mutation ( random , candidate , args ) : rate = args . setdefault ( 'mutation_rate' , 0.1 ) if random . random ( ) < rate : size = len ( candidate ) p = random . randint ( 0 , size - 1 ) q = random . randint ( 0 , size - 1 ) p , q = min ( p , q ) , max ( p , q ) s = candidate [ p : q + 1 ] random . shuffle...
Return the mutants created by scramble mutation on the candidates .
47,656
def gaussian_mutation ( random , candidate , args ) : mut_rate = args . setdefault ( 'mutation_rate' , 0.1 ) mean = args . setdefault ( 'gaussian_mean' , 0.0 ) stdev = args . setdefault ( 'gaussian_stdev' , 1.0 ) bounder = args [ '_ec' ] . bounder mutant = copy . copy ( candidate ) for i , m in enumerate ( mutant ) : i...
Return the mutants created by Gaussian mutation on the candidates .
47,657
def nonuniform_mutation ( random , candidate , args ) : bounder = args [ '_ec' ] . bounder num_gens = args [ '_ec' ] . num_generations max_gens = args [ 'max_generations' ] strength = args . setdefault ( 'mutation_strength' , 1 ) exponent = ( 1.0 - num_gens / float ( max_gens ) ) ** strength mutant = copy . copy ( cand...
Return the mutants produced by nonuniform mutation on the candidates .
47,658
def crossover ( cross ) : @ functools . wraps ( cross ) def inspyred_crossover ( random , candidates , args ) : if len ( candidates ) % 2 == 1 : candidates = candidates [ : - 1 ] moms = candidates [ : : 2 ] dads = candidates [ 1 : : 2 ] children = [ ] for i , ( mom , dad ) in enumerate ( zip ( moms , dads ) ) : cross ....
Return an inspyred crossover function based on the given function .
47,659
def n_point_crossover ( random , mom , dad , args ) : crossover_rate = args . setdefault ( 'crossover_rate' , 1.0 ) num_crossover_points = args . setdefault ( 'num_crossover_points' , 1 ) children = [ ] if random . random ( ) < crossover_rate : num_cuts = min ( len ( mom ) - 1 , num_crossover_points ) cut_points = rand...
Return the offspring of n - point crossover on the candidates .
47,660
def uniform_crossover ( random , mom , dad , args ) : ux_bias = args . setdefault ( 'ux_bias' , 0.5 ) crossover_rate = args . setdefault ( 'crossover_rate' , 1.0 ) children = [ ] if random . random ( ) < crossover_rate : bro = copy . copy ( dad ) sis = copy . copy ( mom ) for i , ( m , d ) in enumerate ( zip ( mom , da...
Return the offspring of uniform crossover on the candidates .
47,661
def partially_matched_crossover ( random , mom , dad , args ) : crossover_rate = args . setdefault ( 'crossover_rate' , 1.0 ) if random . random ( ) < crossover_rate : size = len ( mom ) points = random . sample ( range ( size ) , 2 ) x , y = min ( points ) , max ( points ) bro = copy . copy ( dad ) bro [ x : y + 1 ] =...
Return the offspring of partially matched crossover on the candidates .
47,662
def arithmetic_crossover ( random , mom , dad , args ) : ax_alpha = args . setdefault ( 'ax_alpha' , 0.5 ) ax_points = args . setdefault ( 'ax_points' , None ) crossover_rate = args . setdefault ( 'crossover_rate' , 1.0 ) bounder = args [ '_ec' ] . bounder children = [ ] if random . random ( ) < crossover_rate : bro = ...
Return the offspring of arithmetic crossover on the candidates .
47,663
def blend_crossover ( random , mom , dad , args ) : blx_alpha = args . setdefault ( 'blx_alpha' , 0.1 ) blx_points = args . setdefault ( 'blx_points' , None ) crossover_rate = args . setdefault ( 'crossover_rate' , 1.0 ) bounder = args [ '_ec' ] . bounder children = [ ] if random . random ( ) < crossover_rate : bro = c...
Return the offspring of blend crossover on the candidates .
47,664
def heuristic_crossover ( random , candidates , args ) : crossover_rate = args . setdefault ( 'crossover_rate' , 1.0 ) bounder = args [ '_ec' ] . bounder if len ( candidates ) % 2 == 1 : candidates = candidates [ : - 1 ] population = list ( args [ '_ec' ] . population ) lookup = dict ( zip ( [ pickle . dumps ( p . cand...
Return the offspring of heuristic crossover on the candidates .
47,665
def gravitational_force ( position_a , mass_a , position_b , mass_b ) : distance = distance_between ( position_a , position_b ) angle = math . atan2 ( position_a [ 1 ] - position_b [ 1 ] , position_a [ 0 ] - position_b [ 0 ] ) magnitude = G * mass_a * mass_b / ( distance ** 2 ) sign = - 1 if mass_b > mass_a else 1 x_fo...
Returns the gravitational force between the two bodies a and b .
47,666
def force_on_satellite ( position , mass ) : earth_grav_force = gravitational_force ( position , mass , earth_position , earth_mass ) moon_grav_force = gravitational_force ( position , mass , moon_position , moon_mass ) F_x = earth_grav_force [ 0 ] + moon_grav_force [ 0 ] F_y = earth_grav_force [ 1 ] + moon_grav_force ...
Returns the total gravitational force acting on the body from the Earth and Moon .
47,667
def acceleration_of_satellite ( position , mass ) : F_x , F_y = force_on_satellite ( position , mass ) return F_x / mass , F_y / mass
Returns the acceleration based on all forces acting upon the body .
47,668
def evaluator ( evaluate ) : @ functools . wraps ( evaluate ) def inspyred_evaluator ( candidates , args ) : fitness = [ ] for candidate in candidates : fitness . append ( evaluate ( candidate , args ) ) return fitness inspyred_evaluator . single_evaluation = evaluate return inspyred_evaluator
Return an inspyred evaluator function based on the given function . This function generator takes a function that evaluates only one candidate . The generator handles the iteration over each candidate to be evaluated .
47,669
def parallel_evaluation_pp ( candidates , args ) : import pp logger = args [ '_ec' ] . logger try : evaluator = args [ 'pp_evaluator' ] except KeyError : logger . error ( 'parallel_evaluation_pp requires \'pp_evaluator\' be defined in the keyword arguments list' ) raise secret_key = args . setdefault ( 'pp_secret' , 'i...
Evaluate the candidates in parallel using Parallel Python .
47,670
def parallel_evaluation_mp ( candidates , args ) : import time import multiprocessing logger = args [ '_ec' ] . logger try : evaluator = args [ 'mp_evaluator' ] except KeyError : logger . error ( 'parallel_evaluation_mp requires \'mp_evaluator\' be defined in the keyword arguments list' ) raise try : nprocs = args [ 'm...
Evaluate the candidates in parallel using multiprocessing .
47,671
def allow_ajax ( request ) : if request . META . get ( 'REMOTE_ADDR' , None ) not in settings . INTERNAL_IPS : return False if toolbar_version < LooseVersion ( '1.8' ) and request . get_full_path ( ) . startswith ( DEBUG_TOOLBAR_URL_PREFIX ) and request . GET . get ( 'panel_id' , None ) != 'RequestHistoryPanel' : retur...
Default function to determine whether to show the toolbar on a given page .
47,672
def content ( self ) : toolbars = OrderedDict ( ) for id , toolbar in DebugToolbar . _store . items ( ) : content = { } for panel in toolbar . panels : panel_id = None nav_title = '' nav_subtitle = '' try : panel_id = panel . panel_id nav_title = panel . nav_title nav_subtitle = panel . nav_subtitle ( ) if isinstance (...
Content of the panel when it s displayed in full screen .
47,673
def extract_content ( self , selector = '' , attr = '' , default = '' , connector = '' , * args , ** kwargs ) : try : if selector . lower ( ) == "url" : return self . url if attr . lower ( ) == "text" : tag = self . get_tree_tag ( selector = selector , get_one = True ) content = connector . join ( [ make_ascii ( x ) . ...
Method for performing the content extraction for the particular selector type . \
47,674
def extract_links ( self , selector = '' , * args , ** kwargs ) : try : links = self . get_tree_tag ( selector = selector ) for link in links : next_url = urljoin ( self . url , link . get ( 'href' ) ) yield type ( self ) ( next_url ) except XPathError : raise Exception ( "Invalid %s selector - %s" % ( self . __selecto...
Method for performing the link extraction for the crawler . \
47,675
def extract_tabular ( self , header = '' , prefix = '' , suffix = '' , table_type = '' , * args , ** kwargs ) : if type ( header ) in [ str , unicode ] : try : header_list = self . get_tree_tag ( header ) table_headers = [ prefix + h . text + suffix for h in header_list ] except XPathError : raise Exception ( "Invalid ...
Method for performing the tabular data extraction . \
47,676
def extract_rows ( self , result = { } , selector = '' , table_headers = [ ] , attr = '' , connector = '' , default = '' , verbosity = 0 , * args , ** kwargs ) : result_list = [ ] try : values = self . get_tree_tag ( selector ) if len ( table_headers ) >= len ( values ) : from itertools import izip_longest pairs = izip...
Row data extraction for extract_tabular
47,677
def extract_columns ( self , result = { } , selector = '' , table_headers = [ ] , attr = '' , connector = '' , default = '' , verbosity = 0 , * args , ** kwargs ) : result_list = [ ] try : if type ( selector ) in [ str , unicode ] : selectors = [ selector ] elif type ( selector ) == list : selectors = selector [ : ] el...
Column data extraction for extract_tabular
47,678
def runCLI ( ) : args = docopt ( __doc__ , version = '0.3.0' ) try : check_arguments ( args ) command_list = [ 'genconfig' , 'run' , 'generate' ] select = itemgetter ( 'genconfig' , 'run' , 'generate' ) selectedCommand = command_list [ select ( args ) . index ( True ) ] cmdClass = get_command_class ( selectedCommand ) ...
The starting point for the execution of the Scrapple command line tool .
47,679
def check_arguments ( args ) : projectname_re = re . compile ( r'[^a-zA-Z0-9_]' ) if args [ 'genconfig' ] : if args [ '--type' ] not in [ 'scraper' , 'crawler' ] : raise InvalidType ( "--type has to be 'scraper' or 'crawler'" ) if args [ '--selector' ] not in [ 'xpath' , 'css' ] : raise InvalidSelector ( "--selector ha...
Validates the arguments passed through the CLI commands .
47,680
def form_to_json ( form ) : config = dict ( ) if form [ 'project_name' ] == "" : raise Exception ( 'Project name cannot be empty.' ) if form [ 'selector_type' ] not in [ "css" , "xpath" ] : raise Exception ( 'Selector type has to css or xpath' ) config [ 'project_name' ] = form [ 'project_name' ] config [ 'selector_typ...
Takes the form from the POST request in the web interface and generates the JSON config \ file
47,681
def execute_command ( self ) : try : self . args [ '--verbosity' ] = int ( self . args [ '--verbosity' ] ) if self . args [ '--verbosity' ] not in [ 0 , 1 , 2 ] : raise ValueError if self . args [ '--verbosity' ] > 0 : print ( Back . GREEN + Fore . BLACK + "Scrapple Run" ) print ( Back . RESET + Fore . RESET ) import j...
The run command implements the web content extractor corresponding to the given \ configuration file .
47,682
def traverse_next ( page , nextx , results , tabular_data_headers = [ ] , verbosity = 0 ) : for link in page . extract_links ( selector = nextx [ 'follow_link' ] ) : if verbosity > 0 : print ( '\n' ) print ( Back . YELLOW + Fore . BLUE + "Loading page " , link . url + Back . RESET + Fore . RESET , end = '' ) r = result...
Recursive generator to traverse through the next attribute and \ crawl through the links to be followed .
47,683
def validate_config ( config ) : fields = [ f for f in get_fields ( config ) ] if len ( fields ) != len ( set ( fields ) ) : raise InvalidConfigException ( "Invalid configuration file - %d duplicate field names" % len ( fields ) - len ( set ( fields ) ) ) return True
Validates the extractor configuration file . Ensures that there are no duplicate field names etc .
47,684
def get_fields ( config ) : for data in config [ 'scraping' ] [ 'data' ] : if data [ 'field' ] != '' : yield data [ 'field' ] if 'next' in config [ 'scraping' ] : for n in config [ 'scraping' ] [ 'next' ] : for f in get_fields ( n ) : yield f
Recursive generator that yields the field names in the config file
47,685
def extract_fieldnames ( config ) : fields = [ ] for x in get_fields ( config ) : if x in fields : fields . append ( x + '_' + str ( fields . count ( x ) + 1 ) ) else : fields . append ( x ) return fields
Function to return a list of unique field names from the config file
47,686
def run ( self , dag : DAGCircuit ) -> DAGCircuit : circ = dagcircuit_to_tk ( dag , _DROP_CONDS = self . DROP_CONDS , _BOX_UNKNOWN = self . BOX_UNKNOWN ) circ , circlay = self . process_circ ( circ ) newdag = tk_to_dagcircuit ( circ ) newdag . name = dag . name finlay = dict ( ) for i , qi in enumerate ( circlay ) : fi...
Run one pass of optimisation on the circuit and route for the given backend .
47,687
def _sort_row_col ( qubits : Iterator [ GridQubit ] ) -> List [ GridQubit ] : return sorted ( qubits , key = lambda x : ( x . row , x . col ) )
Sort grid qubits first by row then by column
47,688
def print_setting ( self ) -> str : ret = "\n" ret += "==================== Setting of {} ============================\n" . format ( self . configuration [ 'name' ] ) ret += "{}" . format ( self . setting ) ret += "===============================================================\n" ret += "{}" . format ( self . _var_for...
Presents the QSE settings as a string .
47,689
def _energy_evaluation ( self , operator ) : if self . _quantum_state is not None : input_circuit = self . _quantum_state else : input_circuit = [ self . opt_circuit ] if operator . _paulis : mean_energy , std_energy = operator . evaluate_with_result ( self . _operator_mode , input_circuit , self . _quantum_instance . ...
Evaluate the energy of the current input circuit with respect to the given operator .
47,690
def _run ( self ) -> dict : if not self . _quantum_instance . is_statevector : raise AquaError ( "Can only calculate state for QSE with statevector backends" ) ret = self . _quantum_instance . execute ( self . opt_circuit ) self . ret = ret self . _eval_count = 0 self . _solve ( ) self . _ret [ 'eval_count' ] = self . ...
Runs the QSE algorithm to compute the eigenvalues of the Hamiltonian .
47,691
def whooshee_search ( self , search_string , group = whoosh . qparser . OrGroup , whoosheer = None , match_substrings = True , limit = None , order_by_relevance = 10 ) : if not whoosheer : entities = set ( ) for cd in self . column_descriptions : entities . add ( cd [ 'type' ] ) if self . _join_entities and isinstance ...
Do a fulltext search on the query . Returns a query filtered with results of the fulltext search .
47,692
def search ( cls , search_string , values_of = '' , group = whoosh . qparser . OrGroup , match_substrings = True , limit = None ) : index = Whooshee . get_or_create_index ( _get_app ( cls ) , cls ) prepped_string = cls . prep_search_string ( search_string , match_substrings ) with index . searcher ( ) as searcher : par...
Searches the fields for given search_string . Returns the found records if values_of is left empty else the values of the given columns .
47,693
def create_index ( cls , app , wh ) : if app . extensions [ 'whooshee' ] [ 'memory_storage' ] : storage = RamStorage ( ) index = storage . create_index ( wh . schema ) assert index return index else : index_path = os . path . join ( app . extensions [ 'whooshee' ] [ 'index_path_root' ] , getattr ( wh , 'index_subdir' ,...
Creates and opens an index for the given whoosheer and app . If the index already exists it just opens it otherwise it creates it first .
47,694
def get_or_create_index ( cls , app , wh ) : if wh in app . extensions [ 'whooshee' ] [ 'whoosheers_indexes' ] : return app . extensions [ 'whooshee' ] [ 'whoosheers_indexes' ] [ wh ] index = cls . create_index ( app , wh ) app . extensions [ 'whooshee' ] [ 'whoosheers_indexes' ] [ wh ] = index return index
Gets a previously cached index or creates a new one for the given app and whoosheer .
47,695
def on_commit ( self , changes ) : if _get_config ( self ) [ 'enable_indexing' ] is False : return None for wh in self . whoosheers : if not wh . auto_update : continue writer = None for change in changes : if change [ 0 ] . __class__ in wh . models : method_name = '{0}_{1}' . format ( change [ 1 ] , change [ 0 ] . __c...
Method that gets called when a model is changed . This serves to do the actual index writing .
47,696
def reindex ( self ) : for wh in self . whoosheers : index = type ( self ) . get_or_create_index ( _get_app ( self ) , wh ) writer = index . writer ( timeout = _get_config ( self ) [ 'writer_timeout' ] ) for model in wh . models : method_name = "{0}_{1}" . format ( UPDATE_KWD , model . __name__ . lower ( ) ) for item i...
Reindex all data
47,697
def dump_info ( ) : vultr = Vultr ( API_KEY ) try : logging . info ( 'Listing account info:\n%s' , dumps ( vultr . account . info ( ) , indent = 2 ) ) logging . info ( 'Listing apps:\n%s' , dumps ( vultr . app . list ( ) , indent = 2 ) ) logging . info ( 'Listing backups:\n%s' , dumps ( vultr . backup . list ( ) , inde...
Shows various details about the account & servers
47,698
def update_params ( params , updates ) : params = params . copy ( ) if isinstance ( params , dict ) else dict ( ) params . update ( updates ) return params
Merges updates into params
47,699
def _request_get_helper ( self , url , params = None ) : if not isinstance ( params , dict ) : params = dict ( ) if self . api_key : params [ 'api_key' ] = self . api_key return requests . get ( url , params = params , timeout = 60 )
API GET request helper