code
stringlengths
9
256k
<s> from decimal import Decimal <EOL> try : <EOL> import lxml . etree as ET <EOL> except ImportError : <EOL> import xml . etree . ElementTree as ET <EOL> import qrcode . image . base <EOL> class SvgFragmentImage ( qrcode . image . base . BaseImage ) : <EOL> """<STR_LIT>""" <EOL> _SVG_namespace = "<STR_LIT>" <EOL> kind = "<STR_LIT>" <EOL> allowed_kinds = ( "<STR_LIT>" , ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> ET . register_namespace ( "<STR_LIT>" , self . _SVG_namespace ) <EOL> super ( SvgFragmentImage , self ) . __init__ ( * args , ** kwargs ) <EOL> self . unit_size = self . units ( self . box_size ) <EOL> def drawrect ( self , row , col ) : <EOL> self . _img . append ( self . _rect ( row , col ) ) <EOL> def units ( self , pixels , text = True ) : <EOL> """<STR_LIT>""" <EOL> units = Decimal ( pixels ) / <NUM_LIT:10> <EOL> if not text : <EOL> return units <EOL> return '<STR_LIT>' % units <EOL> def save ( self , stream , kind = None ) : <EOL> self . check_kind ( kind = kind ) <EOL> self . _write ( stream ) <EOL> def new_image ( self , ** kwargs ) : <EOL> return self . _svg ( ) <EOL> def _svg ( self , tag = None , version = '<STR_LIT>' , ** kwargs ) : <EOL> if tag is None : <EOL> tag = ET . QName ( self . _SVG_namespace , "<STR_LIT>" ) <EOL> dimension = self . units ( self . pixel_size ) <EOL> return ET . Element ( <EOL> tag , width = dimension , height = dimension , version = version , <EOL> ** kwargs ) <EOL> def _rect ( self , row , col , tag = None ) : <EOL> if tag is None : <EOL> tag = ET . QName ( self . _SVG_namespace , "<STR_LIT>" ) <EOL> x , y = self . pixel_box ( row , col ) [ <NUM_LIT:0> ] <EOL> return ET . Element ( <EOL> tag , x = self . units ( x ) , y = self . units ( y ) , <EOL> width = self . unit_size , height = self . unit_size ) <EOL> def _write ( self , stream ) : <EOL> ET . ElementTree ( self . _img ) . write ( stream , xml_declaration = False ) <EOL> class SvgImage ( SvgFragmentImage ) : <EOL> """<STR_LIT>""" <EOL> background = None <EOL> def _svg ( self , tag = '<STR_LIT>' , ** kwargs ) : <EOL> svg = super ( SvgImage , self ) . _svg ( tag = tag , ** kwargs ) <EOL> svg . set ( "<STR_LIT>" , self . _SVG_namespace ) <EOL> if self . background : <EOL> svg . append ( <EOL> ET . Element ( <EOL> '<STR_LIT>' , fill = self . background , x = '<STR_LIT:0>' , y = '<STR_LIT:0>' , width = '<STR_LIT>' , <EOL> height = '<STR_LIT>' ) ) <EOL> return svg <EOL> def _rect ( self , row , col ) : <EOL> return super ( SvgImage , self ) . _rect ( row , col , tag = "<STR_LIT>" ) <EOL> def _write ( self , stream ) : <EOL> ET . ElementTree ( self . _img ) . write ( stream , encoding = "<STR_LIT>" , <EOL> xml_declaration = True ) <EOL> class SvgPathImage ( SvgImage ) : <EOL> """<STR_LIT>""" <EOL> QR_PATH_STYLE = '<STR_LIT>' <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . _points = set ( ) <EOL> super ( SvgPathImage , self ) . __init__ ( * args , ** kwargs ) <EOL> def _svg ( self , viewBox = None , ** kwargs ) : <EOL> if viewBox is None : <EOL> dimension = self . units ( self . pixel_size , text = False ) <EOL> viewBox = '<STR_LIT>' % { '<STR_LIT:d>' : dimension } <EOL> return super ( SvgPathImage , self ) . _svg ( viewBox = viewBox , ** kwargs ) <EOL> def drawrect ( self , row , col ) : <EOL> self . _points . add ( ( col , row ) ) <EOL> def _generate_subpaths ( self ) : <EOL> """<STR_LIT>""" <EOL> rect_size = self . units ( self . box_size , text = False ) <EOL> for point in self . _points : <EOL> x_base = self . units ( <EOL> ( point [ <NUM_LIT:0> ] + self . border ) * self . box_size , text = False ) <EOL> y_base = self . units ( <EOL> ( point [ <NUM_LIT:1> ] + self . border ) * self . box_size , text = False ) <EOL> yield ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % dict ( <EOL> x0 = x_base , y0 = y_base , <EOL> x1 = x_base + rect_size , y1 = y_base + rect_size , <EOL> ) ) <EOL> def make_path ( self ) : <EOL> subpaths = self . _generate_subpaths ( ) <EOL> return ET . Element ( <EOL> ET . QName ( "<STR_LIT:path>" ) , <EOL> style = self . QR_PATH_STYLE , <EOL> d = '<STR_LIT:U+0020>' . join ( subpaths ) , <EOL> id = "<STR_LIT>" <EOL> ) <EOL> def _write ( self , stream ) : <EOL> self . _img . append ( self . make_path ( ) ) <EOL> super ( SvgPathImage , self ) . _write ( stream ) <EOL> class SvgFillImage ( SvgImage ) : <EOL> """<STR_LIT>""" <EOL> background = '<STR_LIT>' <EOL> class SvgPathFillImage ( SvgPathImage ) : <EOL> """<STR_LIT>""" <EOL> background = '<STR_LIT>' </s>
<s> import datetime <EOL> import random <EOL> from salmon . metrics import models <EOL> def generate_sample_data ( point_numbers , interval ) : <EOL> """<STR_LIT>""" <EOL> src_names = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> sources = [ ] <EOL> for name in src_names : <EOL> sources . append ( models . Source . objects . get_or_create ( name = name ) [ <NUM_LIT:0> ] ) <EOL> sources . append ( None ) <EOL> metric_names = [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> for source in sources : <EOL> for name in metric_names : <EOL> metric = models . Metric . objects . get_or_create ( source = source , <EOL> name = name ) [ <NUM_LIT:0> ] <EOL> start = datetime . datetime . now ( ) - datetime . timedelta ( <EOL> minutes = interval * point_numbers ) <EOL> for i in range ( point_numbers ) : <EOL> metric . latest_value = random . randint ( <NUM_LIT:1> , <NUM_LIT:100> ) <EOL> metric . last_updated = ( start + <EOL> datetime . timedelta ( minutes = interval * i ) ) <EOL> metric . save ( ) </s>
<s> import os <EOL> from flask import Flask , render_template_string <EOL> from flask_mail import Mail <EOL> from flask_sqlalchemy import SQLAlchemy <EOL> from flask_user import login_required , UserManager , UserMixin , SQLAlchemyAdapter <EOL> class ConfigClass ( object ) : <EOL> SECRET_KEY = os . getenv ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> SQLALCHEMY_DATABASE_URI = os . getenv ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> CSRF_ENABLED = True <EOL> MAIL_USERNAME = os . getenv ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> MAIL_PASSWORD = os . getenv ( '<STR_LIT>' , '<STR_LIT:password>' ) <EOL> MAIL_DEFAULT_SENDER = os . getenv ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> MAIL_SERVER = os . getenv ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> MAIL_PORT = int ( os . getenv ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> MAIL_USE_SSL = int ( os . getenv ( '<STR_LIT>' , True ) ) <EOL> USER_APP_NAME = "<STR_LIT>" <EOL> USER_ENABLE_MULTIPLE_EMAILS = True <EOL> def create_app ( ) : <EOL> """<STR_LIT>""" <EOL> app = Flask ( __name__ ) <EOL> app . config . from_object ( __name__ + '<STR_LIT>' ) <EOL> db = SQLAlchemy ( app ) <EOL> mail = Mail ( app ) <EOL> class User ( db . Model , UserMixin ) : <EOL> id = db . Column ( db . Integer , primary_key = True ) <EOL> username = db . Column ( db . String ( <NUM_LIT:50> ) , nullable = False , unique = True ) <EOL> password = db . Column ( db . String ( <NUM_LIT:255> ) , nullable = False , server_default = '<STR_LIT>' ) <EOL> reset_password_token = db . Column ( db . String ( <NUM_LIT:100> ) , nullable = False , server_default = '<STR_LIT>' ) <EOL> active = db . Column ( '<STR_LIT>' , db . Boolean ( ) , nullable = False , server_default = '<STR_LIT:0>' ) <EOL> first_name = db . Column ( db . String ( <NUM_LIT:100> ) , nullable = False , server_default = '<STR_LIT>' ) <EOL> last_name = db . Column ( db . String ( <NUM_LIT:100> ) , nullable = False , server_default = '<STR_LIT>' ) <EOL> user_emails = db . relationship ( '<STR_LIT>' ) <EOL> class UserEmail ( db . Model ) : <EOL> id = db . Column ( db . Integer , primary_key = True ) <EOL> user_id = db . Column ( db . Integer , db . ForeignKey ( '<STR_LIT>' ) ) <EOL> email = db . Column ( db . String ( <NUM_LIT:255> ) , nullable = False , unique = True ) <EOL> confirmed_at = db . Column ( db . DateTime ( ) ) <EOL> is_primary = db . Column ( db . Boolean ( ) , nullable = False , default = False ) <EOL> user = db . relationship ( '<STR_LIT>' , uselist = False ) <EOL> db . create_all ( ) <EOL> db_adapter = SQLAlchemyAdapter ( db , User , UserEmailClass = UserEmail ) <EOL> user_manager = UserManager ( db_adapter , app ) <EOL> @ app . route ( '<STR_LIT:/>' ) <EOL> def home_page ( ) : <EOL> return render_template_string ( """<STR_LIT>""" ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> @ login_required <EOL> def members_page ( ) : <EOL> return render_template_string ( """<STR_LIT>""" ) <EOL> return app <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> app = create_app ( ) <EOL> app . run ( host = '<STR_LIT>' , port = <NUM_LIT> , debug = True ) </s>
<s> import sys , os , datetime <EOL> extensions = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> templates_path = [ '<STR_LIT>' ] <EOL> source_suffix = '<STR_LIT>' <EOL> master_doc = '<STR_LIT:index>' <EOL> copyright = u'<STR_LIT>' . format ( year = datetime . datetime . today ( ) . year ) <EOL> exclude_patterns = [ '<STR_LIT>' ] <EOL> pygments_style = '<STR_LIT>' <EOL> html_theme = '<STR_LIT>' <EOL> html_static_path = [ '<STR_LIT>' ] <EOL> intersphinx_mapping = { '<STR_LIT>' : None } </s>
<s> import os <EOL> from kazoo . client import KazooClient <EOL> from multiprocessing import Process <EOL> import time <EOL> import zopkio . runtime as runtime <EOL> import zopkio . test_utils as testutilities <EOL> import zopkio . adhoc_deployer as adhoc_deployer <EOL> from zopkio . ztests import ZTestSuite , ZTest <EOL> import zopkio . adhoc_deployer as adhoc_deployer <EOL> import zopkio . runtime as runtime <EOL> class ZooKeeperSuite ( ZTestSuite ) : <EOL> def __init__ ( self , config_dir ) : <EOL> self . config_dir = config_dir <EOL> self . LOGS_DIRECTORY = "<STR_LIT>" <EOL> self . OUTPUT_DIRECTORY = "<STR_LIT>" <EOL> self . test1 = TestProcessTracking ( ) <EOL> def setup_suite ( self ) : <EOL> print "<STR_LIT>" <EOL> env_dict = { } <EOL> if "<STR_LIT:localhost>" not in runtime . get_active_config ( '<STR_LIT>' ) : <EOL> env_dict = { '<STR_LIT>' : '<STR_LIT>' } <EOL> zookeeper_deployer = adhoc_deployer . SSHDeployer ( "<STR_LIT>" , <EOL> { '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : runtime . get_active_config ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : env_dict , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : runtime . get_active_config ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : runtime . get_active_config ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : runtime . get_active_config ( '<STR_LIT>' ) } ) <EOL> runtime . set_deployer ( "<STR_LIT>" , zookeeper_deployer ) <EOL> zookeeper_deployer . install ( "<STR_LIT>" , <EOL> { "<STR_LIT>" : runtime . get_active_config ( '<STR_LIT>' ) , <EOL> "<STR_LIT>" : "<STR_LIT>" } ) <EOL> zookeeper_deployer . start ( "<STR_LIT>" , configs = { "<STR_LIT>" : True } ) <EOL> def teardown_suite ( self ) : <EOL> zookeeper_deployer = runtime . get_deployer ( "<STR_LIT>" ) <EOL> zookeeper_deployer . undeploy ( "<STR_LIT>" ) <EOL> print "<STR_LIT>" <EOL> def process_logs ( self , servicename ) : <EOL> """<STR_LIT>""" <EOL> if servicename == "<STR_LIT>" : <EOL> return [ os . path . join ( "<STR_LIT>" , "<STR_LIT>" ) ] <EOL> else : <EOL> return [ ] <EOL> def naarad_config ( self ) : <EOL> """<STR_LIT>""" <EOL> return os . path . join ( os . path . dirname ( os . path . abspath ( __file__ ) ) , "<STR_LIT>" ) <EOL> def zookeeper_ephemeral_node ( name ) : <EOL> zk = KazooClient ( hosts = str ( runtime . get_active_config ( '<STR_LIT>' ) + '<STR_LIT>' ) ) <EOL> zk . start ( ) <EOL> zk . create ( "<STR_LIT>" , b"<STR_LIT>" , ephemeral = True ) <EOL> time . sleep ( <NUM_LIT:10> ) <EOL> assert zk . exists ( "<STR_LIT>" ) , "<STR_LIT>" <EOL> time . sleep ( <NUM_LIT:20> ) <EOL> zk . stop ( ) <EOL> class TestProcessTracking ( ZTest ) : <EOL> def test ( self ) : <EOL> """<STR_LIT>""" <EOL> time . sleep ( <NUM_LIT:5> ) <EOL> kazoo_connection_url = str ( runtime . get_active_config ( '<STR_LIT>' ) + '<STR_LIT>' ) <EOL> zkclient = KazooClient ( hosts = kazoo_connection_url ) <EOL> zkclient . start ( ) <EOL> zkclient . ensure_path ( "<STR_LIT>" ) <EOL> p = Process ( target = zookeeper_ephemeral_node , args = ( "<STR_LIT>" , ) ) <EOL> p . start ( ) <EOL> zkclient . stop ( ) <EOL> def validate ( self ) : <EOL> """<STR_LIT>""" <EOL> zk = KazooClient ( hosts = str ( runtime . get_active_config ( '<STR_LIT>' ) + '<STR_LIT>' ) ) <EOL> zk . start ( ) <EOL> time . sleep ( <NUM_LIT> ) <EOL> assert not zk . exists ( "<STR_LIT>" ) , "<STR_LIT>" <EOL> zk . stop ( ) <EOL> ztestsuite = ZooKeeperSuite ( os . path . join ( os . path . dirname ( os . path . abspath ( __file__ ) ) , "<STR_LIT>" ) ) </s>
<s> import os <EOL> import unittest <EOL> import zopkio . testobj as testobj <EOL> import datetime <EOL> import time <EOL> import zopkio . runtime as runtime <EOL> class TestTestUtils ( unittest . TestCase ) : <EOL> FILE_LOCATION = os . path . dirname ( os . path . abspath ( __file__ ) ) <EOL> def test_get_log_for_test ( self ) : <EOL> """<STR_LIT>""" <EOL> test = testobj . Test ( "<STR_LIT>" , None , phase = <NUM_LIT:0> , iteration = <NUM_LIT:0> ) <EOL> test . start_time = time . time ( ) <EOL> time . sleep ( <NUM_LIT:2> ) <EOL> test . end_time = time . time ( ) <EOL> runtime . set_active_tests ( [ test ] ) <EOL> output_path = '<STR_LIT>' <EOL> if not os . path . exists ( output_path ) : <EOL> os . mkdir ( output_path ) <EOL> with open ( os . path . join ( output_path , '<STR_LIT>' ) , '<STR_LIT:w>' ) as f : <EOL> f . write ( '<STR_LIT>' ) <EOL> with open ( os . path . join ( output_path , '<STR_LIT>' ) , '<STR_LIT:w>' ) as f : <EOL> f . write ( '<STR_LIT>' ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
<s> def merge_by_average ( hars ) : <EOL> """<STR_LIT>""" <EOL> logs = map ( lambda har : har [ '<STR_LIT>' ] , hars ) <EOL> os_info_first = logs [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> browser_info_first = logs [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> creator_first = logs [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> assert all ( log [ '<STR_LIT>' ] == os_info_first and <EOL> log [ '<STR_LIT>' ] == browser_info_first and <EOL> log [ '<STR_LIT>' ] == creator_first for log in logs ) <EOL> avg_har = { "<STR_LIT>" : { } } <EOL> avg_log = avg_har [ "<STR_LIT>" ] <EOL> avg_log [ '<STR_LIT>' ] = os_info_first <EOL> avg_log [ '<STR_LIT>' ] = creator_first <EOL> avg_log [ '<STR_LIT>' ] = browser_info_first <EOL> avg_log [ '<STR_LIT:version>' ] = logs [ <NUM_LIT:0> ] [ '<STR_LIT:version>' ] <EOL> sorted ( logs , key = lambda log : log [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> median_onload_log = logs [ len ( logs ) / <NUM_LIT:2> ] <EOL> avg_log [ '<STR_LIT>' ] = median_onload_log [ '<STR_LIT>' ] <EOL> avg_log [ '<STR_LIT>' ] = [ ] <EOL> avg_log [ '<STR_LIT>' ] . append ( { } ) <EOL> avg_log [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] = avg_css_stats ( [ log [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] for log in logs ] ) <EOL> avg_log [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] = avg_memory_stats ( [ log [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] for log in logs ] ) <EOL> avg_log [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] = avg_event_stats ( [ log [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] for log in logs ] ) <EOL> avg_log [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] = median_onload_log [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> avg_log [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] = median_onload_log [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> for field , value in median_onload_log [ '<STR_LIT>' ] [ <NUM_LIT:0> ] . iteritems ( ) : <EOL> if type ( value ) == type ( "<STR_LIT>" ) or type ( value ) == type ( u"<STR_LIT>" ) : <EOL> avg_log [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ field ] = value <EOL> return avg_har <EOL> def avg_css_stats ( stats_list ) : <EOL> """<STR_LIT>""" <EOL> css_result = { } <EOL> css_result [ '<STR_LIT>' ] = sum ( stats [ '<STR_LIT>' ] for stats in stats_list ) / len ( stats_list ) <EOL> css_result [ '<STR_LIT>' ] = max ( ( stats [ '<STR_LIT>' ] for stats in stats_list ) , <EOL> key = lambda rule : rule [ '<STR_LIT:time>' ] ) <EOL> css_result [ '<STR_LIT>' ] = max ( ( stats [ '<STR_LIT>' ] for stats in stats_list ) , <EOL> key = lambda rule : rule [ '<STR_LIT>' ] - rule [ '<STR_LIT>' ] ) <EOL> return css_result <EOL> def avg_memory_stats ( stats_list ) : <EOL> """<STR_LIT>""" <EOL> mem_result = { } <EOL> mem_result [ '<STR_LIT>' ] = sum ( stats [ '<STR_LIT>' ] for stats in stats_list ) / len ( stats_list ) <EOL> mem_result [ '<STR_LIT>' ] = max ( stats [ '<STR_LIT>' ] for stats in stats_list ) <EOL> mem_result [ '<STR_LIT>' ] = sum ( stats [ '<STR_LIT>' ] for stats in stats_list ) / len ( stats_list ) <EOL> mem_result [ '<STR_LIT>' ] = max ( stats [ '<STR_LIT>' ] for stats in stats_list ) <EOL> mem_result [ '<STR_LIT>' ] = sum ( stats [ '<STR_LIT>' ] for stats in stats_list ) / len ( stats_list ) <EOL> mem_result [ '<STR_LIT>' ] = max ( stats [ '<STR_LIT>' ] for stats in stats_list ) <EOL> mem_result [ '<STR_LIT>' ] = max ( stats [ '<STR_LIT>' ] for stats in stats_list ) <EOL> mem_result [ '<STR_LIT>' ] = max ( stats [ '<STR_LIT>' ] for stats in stats_list ) <EOL> return mem_result <EOL> def avg_event_stats ( stats_list ) : <EOL> """<STR_LIT>""" <EOL> event_result = { } <EOL> event_result [ '<STR_LIT>' ] = sum ( stats [ '<STR_LIT>' ] for stats in stats_list ) / len ( stats_list ) <EOL> event_result [ '<STR_LIT>' ] = sum ( stats [ '<STR_LIT>' ] for stats in stats_list ) / len ( stats_list ) <EOL> event_result [ '<STR_LIT>' ] = sum ( stats [ '<STR_LIT>' ] for stats in stats_list ) / len ( stats_list ) <EOL> return event_result </s>
<s> """<STR_LIT>""" <EOL> import gc <EOL> import os <EOL> import re <EOL> import logging <EOL> from naarad . metrics . metric import Metric <EOL> import naarad . utils <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> class TopMetric ( Metric ) : <EOL> def __init__ ( self , metric_type , infile , hostname , aggr_metrics , output_directory , resource_path , label , ts_start , ts_end , <EOL> rule_strings , important_sub_metrics , anomaly_detection_metrics , ** other_options ) : <EOL> Metric . __init__ ( self , metric_type , infile , hostname , aggr_metrics , output_directory , resource_path , label , ts_start , ts_end , <EOL> rule_strings , important_sub_metrics , anomaly_detection_metrics ) <EOL> self . PID = [ ] <EOL> self . COMMAND = [ ] <EOL> self . ts_valid_lines = True <EOL> for ( key , val ) in other_options . iteritems ( ) : <EOL> setattr ( self , key , val . split ( ) ) <EOL> self . sub_metrics = None <EOL> self . process_headers = [ ] <EOL> self . ts = '<STR_LIT>' <EOL> self . ts_date = '<STR_LIT>' <EOL> self . ts_time = '<STR_LIT>' <EOL> self . saw_pid = False <EOL> self . data = { } <EOL> for key , val in other_options . iteritems ( ) : <EOL> setattr ( self , key , val . split ( ) ) <EOL> self . sub_metric_description = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> def put_values_into_data ( self , values ) : <EOL> """<STR_LIT>""" <EOL> for col , value in values . items ( ) : <EOL> if col in self . column_csv_map : <EOL> out_csv = self . column_csv_map [ col ] <EOL> else : <EOL> out_csv = self . get_csv ( col ) <EOL> self . data [ out_csv ] = [ ] <EOL> self . data [ out_csv ] . append ( self . ts + "<STR_LIT:U+002C>" + value ) <EOL> def process_top_line ( self , words ) : <EOL> """<STR_LIT>""" <EOL> self . ts_time = words [ <NUM_LIT:2> ] <EOL> self . ts = self . ts_date + '<STR_LIT:U+0020>' + self . ts_time <EOL> self . ts = ts = naarad . utils . get_standardized_timestamp ( self . ts , None ) <EOL> if self . ts_out_of_range ( self . ts ) : <EOL> self . ts_valid_lines = False <EOL> else : <EOL> self . ts_valid_lines = True <EOL> up_days = int ( words [ <NUM_LIT:4> ] ) <EOL> up_hour_minute = words [ <NUM_LIT:6> ] . split ( '<STR_LIT::>' ) <EOL> up_minutes = int ( up_hour_minute [ <NUM_LIT:0> ] ) * <NUM_LIT> + int ( up_hour_minute [ <NUM_LIT:1> ] . split ( '<STR_LIT:U+002C>' ) [ <NUM_LIT:0> ] ) <EOL> uptime_minute = up_days * <NUM_LIT> * <NUM_LIT> + up_minutes <EOL> values = { } <EOL> values [ '<STR_LIT>' ] = str ( uptime_minute ) <EOL> values [ '<STR_LIT>' ] = words [ <NUM_LIT:7> ] <EOL> values [ '<STR_LIT>' ] = words [ <NUM_LIT:11> ] [ : - <NUM_LIT:1> ] <EOL> values [ '<STR_LIT>' ] = words [ <NUM_LIT:12> ] [ : - <NUM_LIT:1> ] <EOL> values [ '<STR_LIT>' ] = words [ <NUM_LIT> ] <EOL> self . put_values_into_data ( values ) <EOL> def process_tasks_line ( self , words ) : <EOL> """<STR_LIT>""" <EOL> words = words [ <NUM_LIT:1> : ] <EOL> length = len ( words ) / <NUM_LIT:2> <EOL> values = { } <EOL> for offset in range ( length ) : <EOL> k = words [ <NUM_LIT:2> * offset + <NUM_LIT:1> ] . strip ( '<STR_LIT:U+002C>' ) <EOL> v = words [ <NUM_LIT:2> * offset ] <EOL> values [ '<STR_LIT>' + k ] = v <EOL> self . put_values_into_data ( values ) <EOL> def process_cpu_line ( self , words ) : <EOL> """<STR_LIT>""" <EOL> values = { } <EOL> for word in words [ <NUM_LIT:1> : ] : <EOL> val , key = word . split ( '<STR_LIT:%>' ) <EOL> values [ '<STR_LIT>' + key . strip ( '<STR_LIT:U+002C>' ) ] = val <EOL> self . put_values_into_data ( values ) <EOL> def convert_to_G ( self , word ) : <EOL> """<STR_LIT>""" <EOL> value = <NUM_LIT:0.0> <EOL> if word [ - <NUM_LIT:1> ] == '<STR_LIT>' or word [ - <NUM_LIT:1> ] == '<STR_LIT:g>' : <EOL> value = float ( word [ : - <NUM_LIT:1> ] ) <EOL> elif word [ - <NUM_LIT:1> ] == '<STR_LIT:M>' or word [ - <NUM_LIT:1> ] == '<STR_LIT:m>' : <EOL> value = float ( word [ : - <NUM_LIT:1> ] ) / <NUM_LIT> <EOL> elif word [ - <NUM_LIT:1> ] == '<STR_LIT>' or word [ - <NUM_LIT:1> ] == '<STR_LIT:k>' : <EOL> value = float ( word [ : - <NUM_LIT:1> ] ) / <NUM_LIT> / <NUM_LIT> <EOL> else : <EOL> value = float ( word ) / <NUM_LIT> / <NUM_LIT> / <NUM_LIT> <EOL> return str ( value ) <EOL> def process_mem_line ( self , words ) : <EOL> """<STR_LIT>""" <EOL> words = words [ <NUM_LIT:1> : ] <EOL> length = len ( words ) / <NUM_LIT:2> <EOL> values = { } <EOL> for offset in range ( length ) : <EOL> k = words [ <NUM_LIT:2> * offset + <NUM_LIT:1> ] . strip ( '<STR_LIT:U+002C>' ) <EOL> v = self . convert_to_G ( words [ <NUM_LIT:2> * offset ] ) <EOL> values [ '<STR_LIT>' + k ] = v <EOL> self . put_values_into_data ( values ) <EOL> def process_swap_line ( self , words ) : <EOL> """<STR_LIT>""" <EOL> words = words [ <NUM_LIT:1> : ] <EOL> length = len ( words ) / <NUM_LIT:2> <EOL> values = { } <EOL> for offset in range ( length ) : <EOL> k = words [ <NUM_LIT:2> * offset + <NUM_LIT:1> ] . strip ( '<STR_LIT:U+002C>' ) <EOL> v = self . convert_to_G ( words [ <NUM_LIT:2> * offset ] ) <EOL> values [ '<STR_LIT>' + k ] = v <EOL> self . put_values_into_data ( values ) <EOL> def process_individual_command ( self , words ) : <EOL> """<STR_LIT>""" <EOL> pid_index = self . process_headers . index ( '<STR_LIT>' ) <EOL> proces_index = self . process_headers . index ( '<STR_LIT>' ) <EOL> pid = words [ pid_index ] <EOL> process = words [ proces_index ] <EOL> if pid in self . PID or process in self . COMMAND : <EOL> process_name = process . split ( '<STR_LIT:/>' ) [ <NUM_LIT:0> ] <EOL> values = { } <EOL> for word_col in self . process_headers : <EOL> word_index = self . process_headers . index ( word_col ) <EOL> if word_col in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> values [ process_name + '<STR_LIT:_>' + pid + '<STR_LIT:_>' + word_col ] = self . convert_to_G ( words [ word_index ] ) <EOL> elif word_col in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> values [ process_name + '<STR_LIT:_>' + pid + '<STR_LIT:_>' + word_col . strip ( '<STR_LIT:%>' ) ] = words [ word_index ] <EOL> uptime_index = self . process_headers . index ( '<STR_LIT>' ) <EOL> uptime = words [ uptime_index ] . split ( '<STR_LIT::>' ) <EOL> uptime_sec = float ( uptime [ <NUM_LIT:0> ] ) * <NUM_LIT> + float ( uptime [ <NUM_LIT:1> ] ) <EOL> values [ process_name + '<STR_LIT:_>' + pid + '<STR_LIT:_>' + '<STR_LIT>' ] = str ( uptime_sec ) <EOL> self . put_values_into_data ( values ) <EOL> def parse ( self ) : <EOL> """<STR_LIT>""" <EOL> for infile in self . infile_list : <EOL> logger . info ( '<STR_LIT>' , infile ) <EOL> status = True <EOL> file_status = naarad . utils . is_valid_file ( infile ) <EOL> if not file_status : <EOL> return False <EOL> with open ( infile ) as fh : <EOL> for line in fh : <EOL> words = line . split ( ) <EOL> if not words : <EOL> continue <EOL> if re . match ( '<STR_LIT>' , line ) : <EOL> self . ts_date = words [ <NUM_LIT:0> ] <EOL> continue <EOL> prefix_word = words [ <NUM_LIT:0> ] . strip ( ) <EOL> if prefix_word == '<STR_LIT>' : <EOL> self . process_top_line ( words ) <EOL> self . saw_pid = False <EOL> elif self . ts_valid_lines : <EOL> if prefix_word == '<STR_LIT>' : <EOL> self . process_tasks_line ( words ) <EOL> elif prefix_word == '<STR_LIT>' : <EOL> self . process_cpu_line ( words ) <EOL> elif prefix_word == '<STR_LIT>' : <EOL> self . process_mem_line ( words ) <EOL> elif prefix_word == '<STR_LIT>' : <EOL> self . process_swap_line ( words ) <EOL> elif prefix_word == '<STR_LIT>' : <EOL> self . saw_pid = True <EOL> self . process_headers = words <EOL> else : <EOL> if self . saw_pid and len ( words ) >= len ( self . process_headers ) : <EOL> self . process_individual_command ( words ) <EOL> for out_csv in self . data . keys ( ) : <EOL> self . csv_files . append ( out_csv ) <EOL> with open ( out_csv , '<STR_LIT:w>' ) as fh : <EOL> fh . write ( '<STR_LIT:\n>' . join ( self . data [ out_csv ] ) ) <EOL> gc . collect ( ) <EOL> return status </s>
<s> """<STR_LIT>""" <EOL> import sys <EOL> import datetime <EOL> class Logger ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , log_config , verbose = False , debug = False ) : <EOL> """<STR_LIT>""" <EOL> self . verbose = verbose <EOL> self . debug = debug <EOL> self . log_config = log_config <EOL> self . logger_file_handle = None <EOL> self . full_log_config_path = None <EOL> self . log_levels = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> def get_current_log_level ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if ( self . log_config [ '<STR_LIT>' ] and <EOL> self . log_config [ '<STR_LIT>' ] in self . log_levels ) : <EOL> return self . log_levels . index ( self . log_config [ '<STR_LIT>' ] ) <EOL> else : <EOL> print ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" . format ( self . log_config [ '<STR_LIT>' ] ) ) <EOL> self . log_config [ '<STR_LIT>' ] = "<STR_LIT>" <EOL> return self . log_levels . index ( self . log_config [ '<STR_LIT>' ] ) <EOL> except KeyError : <EOL> print ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> def is_verbose_logging_enabled ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if self . log_config [ '<STR_LIT>' ] : <EOL> return True <EOL> else : <EOL> return False <EOL> except KeyError : <EOL> print ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> def is_console_logging_enabled ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if self . log_config [ '<STR_LIT>' ] : <EOL> return True <EOL> else : <EOL> return False <EOL> except KeyError : <EOL> print ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> def is_file_logging_enabled ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if self . log_config [ '<STR_LIT:path>' ] : <EOL> return True <EOL> else : <EOL> return False <EOL> except KeyError : <EOL> print ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> def get_logger_file_path ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if self . log_config [ '<STR_LIT:path>' ] : <EOL> return self . log_config [ '<STR_LIT:path>' ] <EOL> else : <EOL> return None <EOL> except KeyError : <EOL> print "<STR_LIT>" <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> def get_logger_file_handle ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . logger_file_handle : <EOL> try : <EOL> file_desc = open ( self . get_logger_file_path ( ) , "<STR_LIT:a>" , <NUM_LIT:0> ) <EOL> self . logger_file_handle = file_desc <EOL> return self . logger_file_handle <EOL> except Exception as exc : <EOL> print ( "<STR_LIT>" <EOL> . format ( self . get_logger_file_path ( ) , exc ) ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> else : <EOL> return self . logger_file_handle <EOL> def logit ( self , log_type , message , log_level = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> if self . get_current_log_level ( ) >= self . log_levels . index ( log_level ) : <EOL> if self . is_file_logging_enabled ( ) : <EOL> self . get_logger_file_handle ( ) . write ( "<STR_LIT>" . format ( datetime . datetime . now ( ) , <EOL> log_type , message ) ) <EOL> if self . is_console_logging_enabled ( ) : <EOL> sys . stdout . write ( "<STR_LIT>" <EOL> . format ( datetime . datetime . now ( ) , <EOL> log_type , message ) ) </s>
<s> """<STR_LIT>""" </s>
<s> import os <EOL> import sys <EOL> from random import randrange <EOL> import platform <EOL> import site <EOL> import redis <EOL> import time <EOL> import signal <EOL> if platform . machine ( ) == "<STR_LIT>" or platform . machine ( ) == "<STR_LIT>" : <EOL> site . addsitedir ( '<STR_LIT>' ) <EOL> else : <EOL> site . addsitedir ( '<STR_LIT>' ) <EOL> import seco . range <EOL> class timeout_exception ( Exception ) : <EOL> pass <EOL> class RedisFinder ( ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , verbose = <NUM_LIT:0> , scope = '<STR_LIT>' , site = None , range_servers = None , return_randomized_servers = True ) : <EOL> if scope : <EOL> self . _scope = scope <EOL> else : <EOL> self . _scope = '<STR_LIT>' <EOL> self . _redis_corelist = [ ] <EOL> self . _site = site <EOL> self . _return_randomized_servers = return_randomized_servers <EOL> self . _cm_conf = open ( '<STR_LIT>' , '<STR_LIT:r>' ) <EOL> self . _range_servers = [ ] <EOL> if range_servers is not None : <EOL> for rs in range_servers . split ( '<STR_LIT:U+002C>' ) : <EOL> self . _range_servers . append ( rs ) <EOL> self . _verbose = verbose <EOL> if self . _verbose : <EOL> print "<STR_LIT>" , self . _scope <EOL> print "<STR_LIT>" , self . _redis_corelist <EOL> print "<STR_LIT>" , self . _site <EOL> print "<STR_LIT>" , self . _cm_conf <EOL> print "<STR_LIT>" , self . _range_servers <EOL> def timeout_handler ( signum , frame ) : <EOL> raise timeout_exception ( ) <EOL> def get_verbose ( self ) : <EOL> return self . _verbose <EOL> def set_verbose ( self , verbose ) : <EOL> self . _verbose = verbose <EOL> if self . _verbose : <EOL> print "<STR_LIT>" , self . _verbose <EOL> def get_scope ( self ) : <EOL> return self . _scope <EOL> def set_scope ( self , scope ) : <EOL> self . _scope = scope <EOL> if self . _verbose : <EOL> print "<STR_LIT>" , self . _scope <EOL> def get_return_randomized_servers ( self ) : <EOL> return self . _return_randomized_servers <EOL> def set_return_randomized_servers ( self , return_randomized_servers ) : <EOL> self . _return_randomized_servers = return_randomized_servers <EOL> if self . _verbose : <EOL> print "<STR_LIT>" , return_randomized_servers <EOL> def get_site ( self ) : <EOL> return self . _site <EOL> def set_site ( self , site ) : <EOL> self . _site = site <EOL> if self . _verbose : <EOL> print "<STR_LIT>" , site <EOL> def discover_site ( self ) : <EOL> self . _cm_conf . seek ( <NUM_LIT:0> ) <EOL> for line in self . _cm_conf . readlines ( ) : <EOL> if "<STR_LIT>" in line : <EOL> self . _site = line . split ( '<STR_LIT:@>' ) [ <NUM_LIT:1> ] . lower ( ) . rstrip ( ) <EOL> if self . _verbose : <EOL> print "<STR_LIT>" , self . _site <EOL> def get_range_servers ( self ) : <EOL> return ( self . _range_servers ) <EOL> def set_range_servers ( self , range_servers ) : <EOL> self . _range_servers = range_servers <EOL> if self . _verbose : <EOL> print "<STR_LIT>" , self . _range_servers <EOL> def discover_range_servers ( self ) : <EOL> self . _cm_conf . seek ( <NUM_LIT:0> ) <EOL> for line in self . _cm_conf . readlines ( ) : <EOL> if "<STR_LIT>" in line : <EOL> self . _range_servers . append ( line . split ( '<STR_LIT::>' ) [ <NUM_LIT:1> ] . rstrip ( ) ) <EOL> if self . _verbose : <EOL> print "<STR_LIT>" , self . _range_servers <EOL> def get_redis_corelist ( self ) : <EOL> return ( self . _redis_corelist ) <EOL> def set_redis_corelist ( self , redis_corelist ) : <EOL> self . _redis_corelist = redis_corelist <EOL> if self . _verbose : <EOL> print "<STR_LIT>" , slef . _redis_corelist <EOL> def query_range_for_redis_corelist ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . _range_servers : <EOL> self . discover_range_servers ( ) <EOL> if self . _scope == "<STR_LIT>" : <EOL> self . _redis_corelist . append ( self . _range_servers [ <NUM_LIT:0> ] ) <EOL> if self . _verbose : <EOL> print "<STR_LIT>" , self . _redis_corelist <EOL> elif self . _scope == "<STR_LIT>" : <EOL> if not self . _site : <EOL> self . discover_site ( ) <EOL> else : <EOL> if self . _verbose : <EOL> print "<STR_LIT>" , self . _site <EOL> range_query = "<STR_LIT>" + self . _site + "<STR_LIT>" <EOL> if self . _verbose : <EOL> print "<STR_LIT>" , range_query <EOL> elif self . _scope == "<STR_LIT>" : <EOL> range_query = "<STR_LIT>" <EOL> if self . _verbose : <EOL> print "<STR_LIT>" , range_query <EOL> else : <EOL> self . _redis_corelist = None <EOL> if self . _verbose : <EOL> print "<STR_LIT>" , self . _scope <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> if self . _scope != "<STR_LIT>" : <EOL> total_redis_corelist = None <EOL> while not total_redis_corelist : <EOL> for range_server in self . _range_servers : <EOL> try : <EOL> range_connection = seco . range . Range ( range_server ) <EOL> if self . _verbose : <EOL> print "<STR_LIT>" , range_server <EOL> total_redis_corelist = range_connection . expand ( <EOL> range_query ) <EOL> if total_redis_corelist : <EOL> if self . _verbose : <EOL> print "<STR_LIT>" , total_redis_corelist <EOL> break <EOL> else : <EOL> if self . _verbose : <EOL> print "<STR_LIT>" , range_server <EOL> except seco . range . RangeException : <EOL> self . _redis_corelist = None <EOL> if self . _verbose : <EOL> print "<STR_LIT>" , range_server <EOL> print "<STR_LIT>" <EOL> if not total_redis_corelist : <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> for single_redis_core in total_redis_corelist : <EOL> redis_servers_single_core = range_connection . expand ( <EOL> "<STR_LIT:%>" + single_redis_core ) <EOL> if self . _verbose : <EOL> print "<STR_LIT>" , redis_servers_single_core <EOL> if not self . _return_randomized_servers : <EOL> for single_redis_server in redis_servers_single_core : <EOL> self . _redis_corelist . append ( single_redis_server ) <EOL> if self . _verbose : <EOL> print "<STR_LIT>" , self . _redis_corelist <EOL> else : <EOL> while redis_servers_single_core : <EOL> corelist_valid = False <EOL> random_redis_server_index = randrange ( <EOL> <NUM_LIT:0> , len ( redis_servers_single_core ) ) <EOL> if self . _verbose : <EOL> print "<STR_LIT>" , random_redis_server_index <EOL> redis_server = redis_servers_single_core . pop ( <EOL> random_redis_server_index ) <EOL> try : <EOL> redis_connection = redis . Redis ( <EOL> host = redis_server , port = <NUM_LIT> , db = <NUM_LIT:0> , socket_timeout = <NUM_LIT:1> , charset = '<STR_LIT:utf-8>' , errors = '<STR_LIT:strict>' ) <EOL> redis_info = redis_connection . info ( ) <EOL> if redis_info : <EOL> self . _redis_corelist . append ( redis_server ) <EOL> if self . _verbose : <EOL> print "<STR_LIT>" , self . _redis_corelist <EOL> corelist_valid = True <EOL> break <EOL> except redis . exceptions . ConnectionError : <EOL> continue <EOL> if not corelist_valid : <EOL> print "<STR_LIT>" + single_redis_core + "<STR_LIT>" <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> def main ( ) : <EOL> print "<STR_LIT>" <EOL> redisResults = RedisFinder ( verbose = <NUM_LIT:1> , scope = '<STR_LIT>' ) <EOL> redisResults . query_range_for_redis_corelist ( ) <EOL> print "<STR_LIT>" <EOL> redisResults = RedisFinder ( verbose = <NUM_LIT:1> , scope = '<STR_LIT>' ) <EOL> redisResults . query_range_for_redis_corelist ( ) <EOL> print "<STR_LIT>" <EOL> redisResults = RedisFinder ( verbose = <NUM_LIT:1> , scope = '<STR_LIT>' ) <EOL> redisResults . query_range_for_redis_corelist ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
<s> from django import forms <EOL> from links . models import Link <EOL> from channels . models import Channel <EOL> class SubmitLinkForm ( forms . ModelForm ) : <EOL> url = forms . URLField ( verify_exists = False , required = False ) <EOL> title = forms . CharField ( <EOL> required = False , <EOL> max_length = <NUM_LIT:255> ) <EOL> thumbnail_url = forms . URLField ( <EOL> required = False , <EOL> widget = forms . HiddenInput ) <EOL> thumbnail_offset = forms . IntegerField ( <EOL> required = False , <EOL> widget = forms . HiddenInput ) <EOL> channel = forms . ModelChoiceField ( <EOL> required = True , <EOL> queryset = Channel . objects . all ( ) ) <EOL> player = forms . CharField ( <EOL> required = False , <EOL> widget = forms . HiddenInput ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( forms . ModelForm , self ) . __init__ ( * args , ** kwargs ) <EOL> self . fields . keyOrder = [ <EOL> '<STR_LIT:url>' , <EOL> '<STR_LIT:title>' , <EOL> '<STR_LIT:description>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> class Meta : <EOL> model = Link <EOL> exclude = [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> class Media : <EOL> js = ( '<STR_LIT>' , ) <EOL> class UpdateLinkForm ( SubmitLinkForm ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( forms . ModelForm , self ) . __init__ ( * args , ** kwargs ) <EOL> self . fields . keyOrder = [ <EOL> '<STR_LIT:title>' , <EOL> '<STR_LIT:description>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> class Meta : <EOL> model = Link <EOL> fields = [ '<STR_LIT:title>' , '<STR_LIT:description>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> class Media : <EOL> js = ( <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ) </s>
<s> import os <EOL> ugettext = lambda s : s <EOL> DEBUG = True <EOL> TEMPLATE_DEBUG = DEBUG <EOL> THUMBNAIL_DEBUG = DEBUG <EOL> PROJECT_ROOT = os . path . dirname ( os . path . abspath ( __file__ . decode ( '<STR_LIT:utf-8>' ) ) ) <EOL> ADMINS = ( ( '<STR_LIT>' , '<STR_LIT>' ) , ) <EOL> SERVER_EMAIL = "<STR_LIT>" <EOL> MANAGERS = ADMINS <EOL> DATABASES = { <EOL> '<STR_LIT:default>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : os . path . join ( PROJECT_ROOT , '<STR_LIT>' ) } } <EOL> TIME_ZONE = '<STR_LIT>' <EOL> LANGUAGE_CODE = '<STR_LIT>' <EOL> SITE_ID = <NUM_LIT:1> <EOL> USE_I18N = True <EOL> USE_L10N = False <EOL> STATIC_ROOT = os . path . join ( PROJECT_ROOT , "<STR_LIT>" ) <EOL> MEDIA_ROOT = os . path . join ( PROJECT_ROOT , "<STR_LIT>" ) <EOL> LOCALE_PATHS = ( os . path . join ( PROJECT_ROOT , "<STR_LIT>" ) , ) <EOL> MEDIA_URL = "<STR_LIT>" <EOL> STATIC_URL = "<STR_LIT>" <EOL> ADMIN_MEDIA_PREFIX = STATIC_URL + '<STR_LIT>' <EOL> STATICFILES_DIRS = ( <EOL> os . path . join ( PROJECT_ROOT , "<STR_LIT>" ) , <EOL> ) <EOL> STATICFILES_FINDERS = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> TEMPLATE_LOADERS = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> MIDDLEWARE_CLASSES = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> INTERNAL_IPS = ( '<STR_LIT:127.0.0.1>' , ) <EOL> TEMPLATE_CONTEXT_PROCESSORS = ( <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> '<STR_LIT>' , <EOL> ) <EOL> ROOT_URLCONF = '<STR_LIT>' <EOL> TEMPLATE_DIRS = ( <EOL> os . path . join ( PROJECT_ROOT , "<STR_LIT>" ) , <EOL> ) <EOL> INSTALLED_APPS = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> STATICFILES_STORAGE = '<STR_LIT>' <EOL> PIPELINE_COMPILERS = ( <EOL> '<STR_LIT>' , <EOL> ) <EOL> PIPELINE_CSS = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : ( <EOL> '<STR_LIT>' , <EOL> ) , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , <EOL> } , <EOL> } <EOL> PIPELINE_LESS_BINARY = "<STR_LIT>" <EOL> SEND_BROKEN_LINK_EMAILS = True <EOL> DEFAULT_FROM_EMAIL = "<STR_LIT>" <EOL> ACCOUNT_ACTIVATION_DAYS = <NUM_LIT:3> <EOL> LOGIN_REDIRECT_URL = "<STR_LIT:/>" <EOL> ABSOLUTE_URL_OVERRIDES = { <EOL> '<STR_LIT>' : lambda user : "<STR_LIT>" % user . username , <EOL> } <EOL> GRAVATAR_DEFAULT_IMAGE = "<STR_LIT>" <EOL> LOGGING = { <EOL> '<STR_LIT:version>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:class>' : '<STR_LIT>' <EOL> } <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True , <EOL> } , <EOL> } <EOL> } <EOL> from local_settings import * </s>
<s> import rethinkdb as r <EOL> import remodel . connection <EOL> run = r . ast . RqlQuery . run <EOL> def remodel_run ( self , c = None , ** global_optargs ) : <EOL> """<STR_LIT>""" <EOL> if not c : <EOL> with remodel . connection . get_conn ( ) as conn : <EOL> return run ( self , conn , ** global_optargs ) <EOL> else : <EOL> return run ( self , c , ** global_optargs ) <EOL> r . ast . RqlQuery . run = remodel_run </s>
<s> import gtk <EOL> import os , sys <EOL> import re <EOL> from types import * <EOL> class uiBuilder ( gtk . Builder ) : <EOL> Gtk_Widget_List = [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' <EOL> ] <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( uiBuilder , self ) . __init__ ( ) <EOL> def add_file ( self , file ) : <EOL> try : <EOL> if os . environ [ "<STR_LIT>" ] . startswith ( "<STR_LIT>" ) : <EOL> self . add_from_file ( file ) <EOL> except KeyError as e : <EOL> self . add_from_file ( file ) <EOL> def get_widget ( self , name = None ) : <EOL> if name : <EOL> if isinstance ( name , basestring ) : <EOL> setattr ( self , name , self . get_object ( name ) ) <EOL> def get_widgets ( self , name = None ) : <EOL> if name : <EOL> if isinstance ( name , dict ) : <EOL> names = [ ] <EOL> for i in name . keys ( ) : <EOL> if i : <EOL> names . append ( i ) <EOL> for i in name . values ( ) : <EOL> if i : <EOL> if isinstance ( i , list ) : <EOL> for j in range ( len ( i ) ) : <EOL> names . append ( i [ j ] ) <EOL> elif isinstance ( i , dict ) : <EOL> pass <EOL> else : <EOL> names . append ( i ) <EOL> for i in range ( len ( names ) ) : <EOL> setattr ( self , names [ i ] , self . get_object ( names [ i ] ) ) <EOL> def connect_widgets ( self , parent ) : <EOL> self . connect_signals ( self ) <EOL> def builder_build ( self , * args , ** kwargs ) : <EOL> widget_list_dict = kwargs . get ( '<STR_LIT>' , { } ) <EOL> def parse_widgets ( file ) : <EOL> ids = re . compile ( "<STR_LIT>" ) <EOL> classes = re . compile ( "<STR_LIT>" ) <EOL> components = { } <EOL> current = '<STR_LIT>' <EOL> with open ( file ) as lines : <EOL> for line in lines . readlines ( ) : <EOL> for id in ids . findall ( line ) : <EOL> if id : <EOL> for klass in classes . findall ( line ) : <EOL> if klass in self . Gtk_Widget_List : <EOL> components [ id ] = [ ] <EOL> current = id <EOL> if not klass in self . Gtk_Widget_List and current : <EOL> try : <EOL> components [ current ] . append ( id ) <EOL> except KeyError : <EOL> print '<STR_LIT>' , current , '<STR_LIT>' , id , '<STR_LIT>' , klass <EOL> return components <EOL> file = kwargs . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if isinstance ( file , list ) : <EOL> for f in file : <EOL> widget_list_dict . update ( parse_widgets ( f ) ) <EOL> self . add_file ( f ) <EOL> self . connect_widgets ( self ) <EOL> elif isinstance ( file , str ) : <EOL> widget_list_dict = parse_widgets ( file ) <EOL> self . add_file ( file ) <EOL> self . connect_widgets ( self ) <EOL> if widget_list_dict : <EOL> self . get_widgets ( widget_list_dict ) <EOL> return True <EOL> else : <EOL> return False <EOL> class uiCreator ( object ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( uiCreator , self ) . __init__ ( ) <EOL> def create_liststore ( self , combobox , items , selected_index = <NUM_LIT:0> , has_empty = True , empty_label = "<STR_LIT>" ) : <EOL> '''<STR_LIT>''' <EOL> liststore = combobox . get_model ( ) <EOL> if not liststore : <EOL> liststore = gtk . ListStore ( str , str ) <EOL> cell = gtk . CellRendererText ( ) <EOL> combobox . pack_start ( cell ) <EOL> combobox . add_attribute ( cell , '<STR_LIT:text>' , <NUM_LIT:0> ) <EOL> combobox . add_attribute ( cell , '<STR_LIT:text>' , <NUM_LIT:0> ) <EOL> else : <EOL> liststore . clear ( ) <EOL> if has_empty : <EOL> liststore . append ( [ str ( empty_label ) , None ] ) <EOL> for p in items : <EOL> liststore . append ( [ items [ p ] , p ] ) <EOL> combobox . set_model ( liststore ) <EOL> combobox . set_active ( selected_index ) <EOL> def get_textview_text ( self , widget ) : <EOL> buffer = widget . get_buffer ( ) <EOL> return buffer . get_text ( buffer . get_start_iter ( ) , buffer . get_end_iter ( ) ) <EOL> def set_textview_text ( self , widget , text ) : <EOL> textbuffer = gtk . TextBuffer ( ) <EOL> textbuffer . set_text ( text ) <EOL> widget . set_buffer ( textbuffer ) <EOL> def set_comboboxes ( self , widget , id ) : <EOL> '''<STR_LIT>''' <EOL> model = widget . get_model ( ) <EOL> i = <NUM_LIT:0> <EOL> if model : <EOL> for m in model : <EOL> iter = model . get_iter ( i ) <EOL> if "<STR_LIT:%s>" % model . get_value ( iter , <NUM_LIT:1> ) == "<STR_LIT:%s>" % id : <EOL> widget . set_active ( i ) <EOL> break <EOL> i += <NUM_LIT:1> <EOL> def get_combobox_selection ( self , widget , type = '<STR_LIT:id>' ) : <EOL> '''<STR_LIT>''' <EOL> model = widget . get_model ( ) <EOL> active = widget . get_active ( ) <EOL> if type != '<STR_LIT:id>' : <EOL> return active <EOL> if active < <NUM_LIT:0> : <EOL> return None <EOL> return model [ active ] [ <NUM_LIT:1> ] </s>
<s> from django . db . models import Q <EOL> from django . db . utils import IntegrityError <EOL> from django . test import TestCase , skipIfDBFeature <EOL> from django . forms . models import modelform_factory <EOL> from . models import ( <EOL> Address , Place , Restaurant , Link , CharLink , TextLink , <EOL> Person , Contact , Note , Organization , OddRelation1 , OddRelation2 , Company , <EOL> Developer , Team , Guild , Tag , Board , HasLinkThing , A , B , C , D ) <EOL> class GenericRelationTests ( TestCase ) : <EOL> def test_inherited_models_content_type ( self ) : <EOL> """<STR_LIT>""" <EOL> p = Place . objects . create ( name = "<STR_LIT>" ) <EOL> r = Restaurant . objects . create ( name = "<STR_LIT>" ) <EOL> l1 = Link . objects . create ( content_object = p ) <EOL> l2 = Link . objects . create ( content_object = r ) <EOL> self . assertEqual ( list ( p . links . all ( ) ) , [ l1 ] ) <EOL> self . assertEqual ( list ( r . links . all ( ) ) , [ l2 ] ) <EOL> def test_reverse_relation_pk ( self ) : <EOL> """<STR_LIT>""" <EOL> p = Person . objects . create ( account = <NUM_LIT> , name = '<STR_LIT>' ) <EOL> Address . objects . create ( street = '<STR_LIT>' , <EOL> city = '<STR_LIT>' , state = '<STR_LIT>' , <EOL> zipcode = '<STR_LIT>' , content_object = p ) <EOL> qs = Person . objects . filter ( addresses__zipcode = '<STR_LIT>' ) <EOL> self . assertEqual ( <NUM_LIT:1> , qs . count ( ) ) <EOL> self . assertEqual ( '<STR_LIT>' , qs [ <NUM_LIT:0> ] . name ) <EOL> def test_charlink_delete ( self ) : <EOL> oddrel = OddRelation1 . objects . create ( name = '<STR_LIT>' ) <EOL> CharLink . objects . create ( content_object = oddrel ) <EOL> oddrel . delete ( ) <EOL> def test_textlink_delete ( self ) : <EOL> oddrel = OddRelation2 . objects . create ( name = '<STR_LIT>' ) <EOL> TextLink . objects . create ( content_object = oddrel ) <EOL> oddrel . delete ( ) <EOL> def test_q_object_or ( self ) : <EOL> """<STR_LIT>""" <EOL> note_contact = Contact . objects . create ( ) <EOL> org_contact = Contact . objects . create ( ) <EOL> Note . objects . create ( note = '<STR_LIT>' , content_object = note_contact ) <EOL> org = Organization . objects . create ( name = '<STR_LIT>' ) <EOL> org . contacts . add ( org_contact ) <EOL> qs = Contact . objects . filter ( Q ( notes__note__icontains = r'<STR_LIT>' ) | <EOL> Q ( organizations__name__icontains = r'<STR_LIT>' ) ) <EOL> self . assertTrue ( org_contact in qs ) <EOL> qs = Contact . objects . filter ( <EOL> Q ( organizations__name__icontains = r'<STR_LIT>' ) | <EOL> Q ( notes__note__icontains = r'<STR_LIT>' ) ) <EOL> self . assertTrue ( org_contact in qs ) <EOL> def test_join_reuse ( self ) : <EOL> qs = Person . objects . filter ( <EOL> addresses__street = '<STR_LIT:foo>' <EOL> ) . filter ( <EOL> addresses__street = '<STR_LIT:bar>' <EOL> ) <EOL> self . assertEqual ( str ( qs . query ) . count ( '<STR_LIT>' ) , <NUM_LIT:2> ) <EOL> def test_generic_relation_ordering ( self ) : <EOL> """<STR_LIT>""" <EOL> p1 = Place . objects . create ( name = "<STR_LIT>" ) <EOL> p2 = Place . objects . create ( name = "<STR_LIT>" ) <EOL> c = Company . objects . create ( name = "<STR_LIT>" ) <EOL> Link . objects . create ( content_object = p1 ) <EOL> Link . objects . create ( content_object = c ) <EOL> places = list ( Place . objects . order_by ( '<STR_LIT>' ) ) <EOL> def count_places ( place ) : <EOL> return len ( [ p for p in places if p . id == place . id ] ) <EOL> self . assertEqual ( len ( places ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( count_places ( p1 ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( count_places ( p2 ) , <NUM_LIT:1> ) <EOL> def test_target_model_is_unsaved ( self ) : <EOL> """<STR_LIT>""" <EOL> dev1 = Developer ( name = '<STR_LIT>' ) <EOL> note = Note ( note = '<STR_LIT>' , content_object = dev1 ) <EOL> self . assertRaises ( IntegrityError , note . save ) <EOL> def test_target_model_len_zero ( self ) : <EOL> """<STR_LIT>""" <EOL> team1 = Team . objects . create ( name = '<STR_LIT>' ) <EOL> try : <EOL> note = Note ( note = '<STR_LIT>' , content_object = team1 ) <EOL> except Exception as e : <EOL> if ( issubclass ( type ( e ) , Exception ) and <EOL> str ( e ) == '<STR_LIT>' ) : <EOL> self . fail ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> raise e <EOL> note . save ( ) <EOL> def test_target_model_nonzero_false ( self ) : <EOL> """<STR_LIT>""" <EOL> g1 = Guild . objects . create ( name = '<STR_LIT>' ) <EOL> note = Note ( note = '<STR_LIT>' , content_object = g1 ) <EOL> note . save ( ) <EOL> @ skipIfDBFeature ( '<STR_LIT>' ) <EOL> def test_gfk_to_model_with_empty_pk ( self ) : <EOL> """<STR_LIT>""" <EOL> b1 = Board . objects . create ( name = '<STR_LIT>' ) <EOL> tag = Tag ( label = '<STR_LIT>' , content_object = b1 ) <EOL> tag . save ( ) <EOL> def test_ticket_20378 ( self ) : <EOL> hs1 = HasLinkThing . objects . create ( ) <EOL> hs2 = HasLinkThing . objects . create ( ) <EOL> l1 = Link . objects . create ( content_object = hs1 ) <EOL> l2 = Link . objects . create ( content_object = hs2 ) <EOL> self . assertQuerysetEqual ( <EOL> HasLinkThing . objects . filter ( links = l1 ) , <EOL> [ hs1 ] , lambda x : x ) <EOL> self . assertQuerysetEqual ( <EOL> HasLinkThing . objects . filter ( links = l2 ) , <EOL> [ hs2 ] , lambda x : x ) <EOL> self . assertQuerysetEqual ( <EOL> HasLinkThing . objects . exclude ( links = l2 ) , <EOL> [ hs1 ] , lambda x : x ) <EOL> self . assertQuerysetEqual ( <EOL> HasLinkThing . objects . exclude ( links = l1 ) , <EOL> [ hs2 ] , lambda x : x ) <EOL> def test_ticket_20564 ( self ) : <EOL> b1 = B . objects . create ( ) <EOL> b2 = B . objects . create ( ) <EOL> b3 = B . objects . create ( ) <EOL> c1 = C . objects . create ( b = b1 ) <EOL> c2 = C . objects . create ( b = b2 ) <EOL> c3 = C . objects . create ( b = b3 ) <EOL> A . objects . create ( flag = None , content_object = b1 ) <EOL> A . objects . create ( flag = True , content_object = b2 ) <EOL> self . assertQuerysetEqual ( <EOL> C . objects . filter ( b__a__flag = None ) , <EOL> [ c1 , c3 ] , lambda x : x <EOL> ) <EOL> self . assertQuerysetEqual ( <EOL> C . objects . exclude ( b__a__flag = None ) , <EOL> [ c2 ] , lambda x : x <EOL> ) <EOL> def test_ticket_20564_nullable_fk ( self ) : <EOL> b1 = B . objects . create ( ) <EOL> b2 = B . objects . create ( ) <EOL> b3 = B . objects . create ( ) <EOL> d1 = D . objects . create ( b = b1 ) <EOL> d2 = D . objects . create ( b = b2 ) <EOL> d3 = D . objects . create ( b = b3 ) <EOL> d4 = D . objects . create ( ) <EOL> A . objects . create ( flag = None , content_object = b1 ) <EOL> A . objects . create ( flag = True , content_object = b1 ) <EOL> A . objects . create ( flag = True , content_object = b2 ) <EOL> self . assertQuerysetEqual ( <EOL> D . objects . exclude ( b__a__flag = None ) , <EOL> [ d2 ] , lambda x : x <EOL> ) <EOL> self . assertQuerysetEqual ( <EOL> D . objects . filter ( b__a__flag = None ) , <EOL> [ d1 , d3 , d4 ] , lambda x : x <EOL> ) <EOL> self . assertQuerysetEqual ( <EOL> B . objects . filter ( a__flag = None ) , <EOL> [ b1 , b3 ] , lambda x : x <EOL> ) <EOL> self . assertQuerysetEqual ( <EOL> B . objects . exclude ( a__flag = None ) , <EOL> [ b2 ] , lambda x : x <EOL> ) <EOL> def test_extra_join_condition ( self ) : <EOL> self . assertIn ( "<STR_LIT>" , str ( B . objects . exclude ( a__flag = None ) . query ) . lower ( ) ) <EOL> self . assertNotIn ( "<STR_LIT>" , str ( B . objects . exclude ( a__flag = True ) . query ) . lower ( ) ) <EOL> self . assertIn ( "<STR_LIT>" , str ( B . objects . exclude ( a__flag = True ) . query ) . lower ( ) ) <EOL> def test_editable_generic_rel ( self ) : <EOL> GenericRelationForm = modelform_factory ( HasLinkThing , fields = '<STR_LIT>' ) <EOL> form = GenericRelationForm ( ) <EOL> self . assertIn ( '<STR_LIT>' , form . fields ) <EOL> form = GenericRelationForm ( { '<STR_LIT>' : None } ) <EOL> self . assertTrue ( form . is_valid ( ) ) <EOL> form . save ( ) <EOL> links = HasLinkThing . _meta . get_field_by_name ( '<STR_LIT>' ) [ <NUM_LIT:0> ] . field <EOL> self . assertEqual ( links . save_form_data_calls , <NUM_LIT:1> ) </s>
<s> import urllib2 <EOL> class PinboardError ( Exception ) : <EOL> pass <EOL> class PinboardServerError ( urllib2 . HTTPError ) : <EOL> pass <EOL> class PinboardServiceUnavailable ( urllib2 . HTTPError ) : <EOL> pass <EOL> class PinboardAuthenticationError ( urllib2 . HTTPError ) : <EOL> pass <EOL> class PinboardForbiddenError ( urllib2 . HTTPError ) : <EOL> pass </s>
<s> import operator <EOL> from datetime import datetime <EOL> from datetime import timedelta <EOL> import logging <EOL> from google . appengine . api import mail <EOL> from google . appengine . ext import deferred <EOL> from google . appengine . ext import ndb <EOL> import flask <EOL> import github <EOL> import config <EOL> import util <EOL> import model <EOL> def send_mail_notification ( subject , body , to = None , ** kwargs ) : <EOL> if not config . CONFIG_DB . feedback_email : <EOL> return <EOL> brand_name = config . CONFIG_DB . brand_name <EOL> sender = '<STR_LIT>' % ( brand_name , config . CONFIG_DB . feedback_email ) <EOL> subject = '<STR_LIT>' % ( brand_name , subject ) <EOL> if config . DEVELOPMENT : <EOL> logging . info ( <EOL> '<STR_LIT:\n>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % ( sender , to or sender , subject , body ) <EOL> ) <EOL> deferred . defer ( mail . send_mail , sender , to or sender , subject , body , ** kwargs ) <EOL> def new_user_notification ( user_db ) : <EOL> if not config . CONFIG_DB . notify_on_new_user : <EOL> return <EOL> body = '<STR_LIT>' % ( <EOL> user_db . name , <EOL> user_db . username , <EOL> user_db . email , <EOL> '<STR_LIT>' . join ( [ '<STR_LIT>' . join ( ( '<STR_LIT>' % a ) . split ( '<STR_LIT:_>' ) ) for a in user_db . auth_ids ] ) , <EOL> flask . url_for ( '<STR_LIT>' , user_id = user_db . key . id ( ) , _external = True ) , <EOL> ) <EOL> if user_db . github : <EOL> body += '<STR_LIT>' % ( flask . url_for ( '<STR_LIT>' , username = user_db . github , _external = True ) ) <EOL> send_mail_notification ( '<STR_LIT>' % user_db . name , body ) <EOL> def verify_email_notification ( user_db ) : <EOL> if not ( config . CONFIG_DB . verify_email and user_db . email ) or user_db . verified : <EOL> return <EOL> user_db . token = util . uuid ( ) <EOL> user_db . put ( ) <EOL> to = '<STR_LIT>' % ( user_db . name , user_db . email ) <EOL> body = '''<STR_LIT>''' % { <EOL> '<STR_LIT:name>' : user_db . name , <EOL> '<STR_LIT>' : flask . url_for ( '<STR_LIT>' , token = user_db . token , _external = True ) , <EOL> '<STR_LIT>' : config . CONFIG_DB . brand_name , <EOL> } <EOL> flask . flash ( <EOL> '<STR_LIT>' , <EOL> category = '<STR_LIT:success>' , <EOL> ) <EOL> send_mail_notification ( '<STR_LIT>' , body , to ) <EOL> def reset_password_notification ( user_db ) : <EOL> if not user_db . email : <EOL> return <EOL> user_db . token = util . uuid ( ) <EOL> user_db . put ( ) <EOL> to = '<STR_LIT>' % ( user_db . name , user_db . email ) <EOL> body = '''<STR_LIT>''' % { <EOL> '<STR_LIT:name>' : user_db . name , <EOL> '<STR_LIT>' : flask . url_for ( '<STR_LIT>' , token = user_db . token , _external = True ) , <EOL> '<STR_LIT>' : config . CONFIG_DB . brand_name , <EOL> } <EOL> flask . flash ( <EOL> '<STR_LIT>' , <EOL> category = '<STR_LIT:success>' , <EOL> ) <EOL> send_mail_notification ( '<STR_LIT>' , body , to ) <EOL> def activate_user_notification ( user_db ) : <EOL> if not user_db . email : <EOL> return <EOL> user_db . token = util . uuid ( ) <EOL> user_db . put ( ) <EOL> to = user_db . email <EOL> body = '''<STR_LIT>''' % { <EOL> '<STR_LIT>' : flask . url_for ( '<STR_LIT>' , token = user_db . token , _external = True ) , <EOL> '<STR_LIT>' : config . CONFIG_DB . brand_name , <EOL> } <EOL> flask . flash ( <EOL> '<STR_LIT>' , <EOL> category = '<STR_LIT:success>' , <EOL> ) <EOL> send_mail_notification ( '<STR_LIT>' , body , to ) <EOL> def email_conflict_notification ( email ) : <EOL> body = '<STR_LIT>' % ( <EOL> email , <EOL> flask . url_for ( '<STR_LIT>' , email = email , _external = True ) , <EOL> ) <EOL> send_mail_notification ( '<STR_LIT>' % email , body ) <EOL> def queue_account ( account_db ) : <EOL> if account_db . status in [ '<STR_LIT>' ] : <EOL> return <EOL> max_repos = <NUM_LIT> <EOL> queue_it = False <EOL> delta = ( datetime . utcnow ( ) - account_db . modified ) <EOL> if account_db . status in [ '<STR_LIT>' , '<STR_LIT:error>' ] : <EOL> account_db . status = '<STR_LIT>' <EOL> account_db . put ( ) <EOL> queue_it = True <EOL> elif delta . days > <NUM_LIT:0> and account_db . status == '<STR_LIT>' and account_db . public_repos < max_repos : <EOL> account_db . status = '<STR_LIT>' <EOL> account_db . put ( ) <EOL> queue_it = True <EOL> elif account_db . status == '<STR_LIT>' : <EOL> if delta . seconds > <NUM_LIT> * <NUM_LIT> or account_db . public_repos > max_repos : <EOL> account_db . status = '<STR_LIT>' <EOL> account_db . put ( ) <EOL> elif delta . seconds > <NUM_LIT:30> * <NUM_LIT> : <EOL> queue_it = True <EOL> if ( delta . seconds > <NUM_LIT:6> * <NUM_LIT> * <NUM_LIT> or delta . days > <NUM_LIT:0> ) and account_db . status != '<STR_LIT>' : <EOL> account_db . status = '<STR_LIT>' <EOL> account_db . put ( ) <EOL> queue_it = True <EOL> if queue_it : <EOL> deferred . defer ( sync_account , account_db ) <EOL> def sync_account ( account_db ) : <EOL> g = github . Github ( config . CONFIG_DB . github_username , config . CONFIG_DB . github_password ) <EOL> try : <EOL> account = g . get_user ( account_db . username ) <EOL> except github . GithubException as error : <EOL> account_db . status = '<STR_LIT:error>' <EOL> if error . status in [ <NUM_LIT> ] : <EOL> account_db . status = str ( error . status ) <EOL> account_db . put ( ) <EOL> return <EOL> account_db . name = account . name or account . login <EOL> account_db . followers = account . followers <EOL> account_db . email = account . email or '<STR_LIT>' <EOL> account_db . public_repos = account . public_repos <EOL> account_db . joined = account . created_at <EOL> account_db . organization = account . type == '<STR_LIT>' <EOL> account_db . avatar_url = account . avatar_url . split ( '<STR_LIT:?>' ) [ <NUM_LIT:0> ] <EOL> account_db . put ( ) <EOL> stars = <NUM_LIT:0> <EOL> forks = <NUM_LIT:0> <EOL> repo_dbs = [ ] <EOL> languages = { } <EOL> for repo in account . get_repos ( ) : <EOL> name = repo . name . lower ( ) <EOL> if name in config . ILLEGAL_KEYS : <EOL> name = '<STR_LIT>' % name <EOL> repo_db = model . Repo . get_or_insert ( <EOL> name , <EOL> parent = account_db . key , <EOL> name = repo . name , <EOL> description = repo . description [ : <NUM_LIT> ] if repo . description else '<STR_LIT>' , <EOL> stars = repo . stargazers_count , <EOL> fork = repo . fork , <EOL> forks = repo . forks_count , <EOL> language = repo . language or '<STR_LIT>' , <EOL> avatar_url = account_db . avatar_url , <EOL> account_username = account_db . username , <EOL> ) <EOL> repo_db . description = repo . description [ : <NUM_LIT> ] if repo . description else '<STR_LIT>' <EOL> repo_db . fork = repo . fork <EOL> repo_db . forks = repo . forks_count <EOL> repo_db . language = repo . language or '<STR_LIT>' <EOL> repo_db . name = repo . name <EOL> repo_db . stars = repo . stargazers_count <EOL> stars += repo_db . stars <EOL> forks += repo_db . forks <EOL> repo_dbs . append ( repo_db ) <EOL> if repo_db . language and not repo_db . fork : <EOL> if repo_db . language not in languages : <EOL> languages [ repo_db . language ] = repo_db . stars + <NUM_LIT:1> <EOL> else : <EOL> languages [ repo_db . language ] += repo_db . stars + <NUM_LIT:1> <EOL> if repo_dbs : <EOL> ndb . put_multi_async ( repo_dbs ) <EOL> account_db . language = max ( languages . iteritems ( ) , key = operator . itemgetter ( <NUM_LIT:1> ) ) [ <NUM_LIT:0> ] if languages else '<STR_LIT>' <EOL> account_db . status = '<STR_LIT>' <EOL> account_db . stars = stars <EOL> account_db . forks = forks <EOL> account_db . put ( ) <EOL> def queue_repo_cleanup ( days = <NUM_LIT:5> ) : <EOL> deferred . defer ( repo_cleanup , days ) <EOL> def repo_cleanup ( days , cursor = None ) : <EOL> before_date = datetime . utcnow ( ) - timedelta ( days = days ) <EOL> repo_qry = model . Repo . query ( ) . filter ( model . Repo . modified < before_date ) <EOL> repo_keys , repo_cursors = util . get_dbs ( <EOL> repo_qry , <EOL> order = '<STR_LIT>' , <EOL> keys_only = True , <EOL> cursor = cursor , <EOL> ) <EOL> ndb . delete_multi ( repo_keys ) <EOL> if repo_cursors [ '<STR_LIT>' ] : <EOL> deferred . defer ( repo_cleanup , days , repo_cursors [ '<STR_LIT>' ] ) </s>
<s> import flask <EOL> import auth <EOL> import config <EOL> import model <EOL> import util <EOL> from main import app <EOL> linkedin_config = dict ( <EOL> access_token_method = '<STR_LIT:POST>' , <EOL> access_token_url = '<STR_LIT>' , <EOL> authorize_url = '<STR_LIT>' , <EOL> base_url = '<STR_LIT>' , <EOL> consumer_key = config . CONFIG_DB . linkedin_api_key , <EOL> consumer_secret = config . CONFIG_DB . linkedin_secret_key , <EOL> request_token_params = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:state>' : util . uuid ( ) , <EOL> } , <EOL> ) <EOL> linkedin = auth . create_oauth_app ( linkedin_config , '<STR_LIT>' ) <EOL> def change_linkedin_query ( uri , headers , body ) : <EOL> headers [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return uri , headers , body <EOL> linkedin . pre_request = change_linkedin_query <EOL> @ app . route ( '<STR_LIT>' ) <EOL> def linkedin_authorized ( ) : <EOL> response = linkedin . authorized_response ( ) <EOL> if response is None : <EOL> flask . flash ( '<STR_LIT>' ) <EOL> return flask . redirect ( util . get_next_url ( ) ) <EOL> flask . session [ '<STR_LIT>' ] = ( response [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> me = linkedin . get ( '<STR_LIT>' ) <EOL> user_db = retrieve_user_from_linkedin ( me . data ) <EOL> return auth . signin_user_db ( user_db ) <EOL> @ linkedin . tokengetter <EOL> def get_linkedin_oauth_token ( ) : <EOL> return flask . session . get ( '<STR_LIT>' ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> def signin_linkedin ( ) : <EOL> return auth . signin_oauth ( linkedin ) <EOL> def retrieve_user_from_linkedin ( response ) : <EOL> auth_id = '<STR_LIT>' % response [ '<STR_LIT:id>' ] <EOL> user_db = model . User . get_by ( '<STR_LIT>' , auth_id ) <EOL> if user_db : <EOL> return user_db <EOL> names = [ response . get ( '<STR_LIT>' , '<STR_LIT>' ) , response . get ( '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> name = '<STR_LIT:U+0020>' . join ( names ) . strip ( ) <EOL> email = response . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> return auth . create_user_db ( <EOL> auth_id = auth_id , <EOL> name = name , <EOL> username = email or name , <EOL> email = email , <EOL> verified = bool ( email ) , <EOL> ) </s>
<s> import os <EOL> import platform <EOL> from distutils . core import setup <EOL> def get_version ( ) : <EOL> """<STR_LIT>""" <EOL> if os . path . isdir ( "<STR_LIT>" ) : <EOL> os . system ( '<STR_LIT>' ) <EOL> f = open ( '<STR_LIT>' ) <EOL> else : <EOL> f = open ( '<STR_LIT>' ) <EOL> version = '<STR_LIT>' . join ( f . readlines ( ) ) . rstrip ( ) <EOL> f . close ( ) <EOL> return version <EOL> data_files = [ ( '<STR_LIT>' , [ '<STR_LIT>' ] ) ] <EOL> if platform . dist ( ) [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> data_files . append ( ( '<STR_LIT>' , [ '<STR_LIT>' ] ) ) <EOL> if platform . dist ( ) [ <NUM_LIT:0> ] in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> data_files . append ( ( '<STR_LIT>' , [ '<STR_LIT>' ] ) ) <EOL> data_files . append ( ( '<STR_LIT>' , [ '<STR_LIT>' ] ) ) <EOL> data_files . append ( ( '<STR_LIT>' , [ '<STR_LIT>' ] ) ) <EOL> setup ( name = '<STR_LIT>' , <EOL> author = '<STR_LIT>' , <EOL> author_email = '<STR_LIT>' , <EOL> data_files = data_files , <EOL> description = '<STR_LIT>' , <EOL> packages = [ '<STR_LIT>' ] , <EOL> scripts = [ "<STR_LIT>" ] , <EOL> url = '<STR_LIT>' , <EOL> version = get_version ( ) , <EOL> ) </s>
<s> from __future__ import print_function <EOL> from collections import defaultdict <EOL> import inspect <EOL> import getopt <EOL> import os <EOL> import shutil <EOL> import sys <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> throot = "<STR_LIT:/>" . join ( sys . path [ <NUM_LIT:0> ] . split ( "<STR_LIT:/>" ) [ : - <NUM_LIT:2> ] ) <EOL> options = defaultdict ( bool ) <EOL> options . update ( dict ( [ x , y or True ] for x , y in getopt . getopt ( sys . argv [ <NUM_LIT:1> : ] , '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:test>' ] ) [ <NUM_LIT:0> ] ) ) <EOL> if options [ '<STR_LIT>' ] : <EOL> print ( '<STR_LIT>' % sys . argv [ <NUM_LIT:0> ] ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' ) <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> options [ '<STR_LIT>' ] = not options [ '<STR_LIT>' ] <EOL> def mkdir ( path ) : <EOL> try : <EOL> os . mkdir ( path ) <EOL> except OSError : <EOL> pass <EOL> outdir = options [ '<STR_LIT>' ] or ( throot + '<STR_LIT>' ) <EOL> mkdir ( outdir ) <EOL> os . chdir ( outdir ) <EOL> mkdir ( "<STR_LIT>" ) <EOL> pythonpath = os . environ . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> pythonpath = throot + '<STR_LIT::>' + pythonpath <EOL> os . environ [ '<STR_LIT>' ] = pythonpath <EOL> if options [ '<STR_LIT>' ] : <EOL> import sphinx <EOL> sys . path [ <NUM_LIT:0> : <NUM_LIT:0> ] = [ os . path . join ( throot , '<STR_LIT>' ) ] <EOL> out = sphinx . main ( [ '<STR_LIT>' , '<STR_LIT>' '<STR_LIT:text>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , os . path . join ( throot , '<STR_LIT>' ) , '<STR_LIT:.>' ] ) <EOL> sys . exit ( out ) <EOL> elif options [ '<STR_LIT>' ] or options [ '<STR_LIT>' ] : <EOL> import sphinx <EOL> sys . path [ <NUM_LIT:0> : <NUM_LIT:0> ] = [ os . path . join ( throot , '<STR_LIT>' ) ] <EOL> sphinx . main ( [ '<STR_LIT>' , '<STR_LIT>' , os . path . join ( throot , '<STR_LIT>' ) , '<STR_LIT:.>' ] ) <EOL> if not options [ '<STR_LIT>' ] : <EOL> import tempfile <EOL> workdir = tempfile . mkdtemp ( ) <EOL> sphinx . main ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> os . path . join ( throot , '<STR_LIT>' ) , workdir ] ) <EOL> os . chdir ( workdir ) <EOL> os . system ( '<STR_LIT>' ) <EOL> try : <EOL> shutil . copy ( os . path . join ( workdir , '<STR_LIT>' ) , outdir ) <EOL> os . chdir ( outdir ) <EOL> shutil . rmtree ( workdir ) <EOL> except OSError as e : <EOL> print ( '<STR_LIT>' , e ) <EOL> except IOError as e : <EOL> print ( '<STR_LIT>' , e ) </s>
<s> """<STR_LIT>""" <EOL> __author__ = "<STR_LIT>" <EOL> __copyright__ = "<STR_LIT>" <EOL> __license__ = "<STR_LIT>" <EOL> from pylearn2 . models . mlp import Layer , PretrainedLayer <EOL> class PretrainedLayerCV ( Layer ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , layer_name , layer_content ) : <EOL> self . layer_name = layer_name <EOL> self . _folds = [ PretrainedLayer ( layer_name , subset_content ) <EOL> for subset_content in layer_content ] <EOL> def select_fold ( self , k ) : <EOL> """<STR_LIT>""" <EOL> return self . _folds [ k ] <EOL> def set_input_space ( self , space ) : <EOL> """<STR_LIT>""" <EOL> return [ fold . set_input_space ( space ) for fold in self . _folds ] <EOL> def get_params ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _folds [ <NUM_LIT:0> ] . get_params ( ) <EOL> def get_input_space ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _folds [ <NUM_LIT:0> ] . get_input_space ( ) <EOL> def get_output_space ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _folds [ <NUM_LIT:0> ] . get_output_space ( ) <EOL> def get_monitoring_channels ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _folds [ <NUM_LIT:0> ] . get_monitoring_channels ( ) </s>
<s> """<STR_LIT>""" <EOL> __authors__ = "<STR_LIT>" <EOL> __copyright__ = "<STR_LIT>" <EOL> __credits__ = [ "<STR_LIT>" ] <EOL> __license__ = "<STR_LIT>" <EOL> __maintainer__ = "<STR_LIT>" <EOL> __email__ = "<STR_LIT>" <EOL> import numpy as np <EOL> from theano import config <EOL> from pylearn2 . datasets import DenseDesignMatrix <EOL> from pylearn2 . utils . rng import make_np_rng <EOL> def _four_regions_labels ( points ) : <EOL> """<STR_LIT>""" <EOL> points = np . asarray ( points ) <EOL> region = np . zeros ( points . shape [ <NUM_LIT:0> ] , dtype = '<STR_LIT>' ) <EOL> tophalf = points [ : , <NUM_LIT:1> ] > <NUM_LIT:0> <EOL> righthalf = points [ : , <NUM_LIT:0> ] > <NUM_LIT:0> <EOL> dists = np . sqrt ( np . sum ( points ** <NUM_LIT:2> , axis = <NUM_LIT:1> ) ) <EOL> region [ dists > np . sqrt ( <NUM_LIT:2> ) ] = <NUM_LIT:255> <EOL> outer = dists > <NUM_LIT> / <NUM_LIT> <EOL> region [ np . logical_and ( tophalf , outer ) ] = <NUM_LIT:3> <EOL> region [ np . logical_and ( np . logical_not ( tophalf ) , outer ) ] = <NUM_LIT:0> <EOL> firstring = np . logical_and ( dists > <NUM_LIT:1.> / <NUM_LIT> , dists <= <NUM_LIT:1.> / <NUM_LIT> ) <EOL> secondring = np . logical_and ( dists > <NUM_LIT:1.> / <NUM_LIT> , dists <= <NUM_LIT> / <NUM_LIT> ) <EOL> region [ np . logical_and ( firstring , righthalf ) ] = <NUM_LIT:2> <EOL> region [ np . logical_and ( secondring , np . logical_not ( righthalf ) ) ] = <NUM_LIT:2> <EOL> region [ np . logical_and ( secondring , righthalf ) ] = <NUM_LIT:1> <EOL> region [ np . logical_and ( np . logical_not ( righthalf ) , dists < <NUM_LIT:1.> / <NUM_LIT> ) ] = <NUM_LIT:1> <EOL> region [ np . logical_and ( righthalf , dists < <NUM_LIT:1.> / <NUM_LIT> ) ] = <NUM_LIT:1> <EOL> assert np . all ( region >= <NUM_LIT:0> ) and np . all ( region <= <NUM_LIT:3> ) <EOL> return region <EOL> class FourRegions ( DenseDesignMatrix ) : <EOL> """<STR_LIT>""" <EOL> _default_seed = ( <NUM_LIT> , <NUM_LIT:5> , <NUM_LIT> ) <EOL> def __init__ ( self , num_examples , rng = ( <NUM_LIT> , <NUM_LIT:5> , <NUM_LIT> ) ) : <EOL> """<STR_LIT>""" <EOL> rng = make_np_rng ( rng , self . _default_seed , which_method = '<STR_LIT>' ) <EOL> X = rng . uniform ( - <NUM_LIT:1> , <NUM_LIT:1> , size = ( num_examples , <NUM_LIT:2> ) ) <EOL> y = _four_regions_labels ( X ) <EOL> super ( FourRegions , self ) . __init__ ( X = X , y = y , y_labels = <NUM_LIT:4> ) </s>
<s> """<STR_LIT>""" <EOL> import numpy as np <EOL> import os <EOL> import tempfile <EOL> from pylearn2 . config import yaml_parse <EOL> from pylearn2 . testing . datasets import ( <EOL> random_dense_design_matrix , <EOL> random_one_hot_dense_design_matrix , <EOL> random_one_hot_topological_dense_design_matrix ) <EOL> from pylearn2 . testing . skip import skip_if_no_h5py <EOL> def test_hdf5_design_matrix ( ) : <EOL> """<STR_LIT>""" <EOL> skip_if_no_h5py ( ) <EOL> import h5py <EOL> handle , filename = tempfile . mkstemp ( ) <EOL> dataset = random_one_hot_dense_design_matrix ( np . random . RandomState ( <NUM_LIT:1> ) , <EOL> num_examples = <NUM_LIT:10> , dim = <NUM_LIT:5> , <EOL> num_classes = <NUM_LIT:3> ) <EOL> with h5py . File ( filename , '<STR_LIT:w>' ) as f : <EOL> f . create_dataset ( '<STR_LIT:X>' , data = dataset . get_design_matrix ( ) ) <EOL> f . create_dataset ( '<STR_LIT:y>' , data = dataset . get_targets ( ) ) <EOL> trainer = yaml_parse . load ( design_matrix_yaml % { '<STR_LIT:filename>' : filename } ) <EOL> trainer . main_loop ( ) <EOL> os . remove ( filename ) <EOL> def test_hdf5_topo_view ( ) : <EOL> """<STR_LIT>""" <EOL> skip_if_no_h5py ( ) <EOL> import h5py <EOL> handle , filename = tempfile . mkstemp ( ) <EOL> dataset = random_one_hot_topological_dense_design_matrix ( <EOL> np . random . RandomState ( <NUM_LIT:1> ) , num_examples = <NUM_LIT:10> , shape = ( <NUM_LIT:2> , <NUM_LIT:2> ) , channels = <NUM_LIT:3> , <EOL> axes = ( '<STR_LIT:b>' , <NUM_LIT:0> , <NUM_LIT:1> , '<STR_LIT:c>' ) , num_classes = <NUM_LIT:3> ) <EOL> with h5py . File ( filename , '<STR_LIT:w>' ) as f : <EOL> f . create_dataset ( '<STR_LIT>' , data = dataset . get_topological_view ( ) ) <EOL> f . create_dataset ( '<STR_LIT:y>' , data = dataset . get_targets ( ) ) <EOL> trainer = yaml_parse . load ( topo_view_yaml % { '<STR_LIT:filename>' : filename } ) <EOL> trainer . main_loop ( ) <EOL> os . remove ( filename ) <EOL> def test_hdf5_convert_to_one_hot ( ) : <EOL> """<STR_LIT>""" <EOL> skip_if_no_h5py ( ) <EOL> import h5py <EOL> handle , filename = tempfile . mkstemp ( ) <EOL> dataset = random_dense_design_matrix ( np . random . RandomState ( <NUM_LIT:1> ) , <EOL> num_examples = <NUM_LIT:10> , dim = <NUM_LIT:5> , num_classes = <NUM_LIT:3> ) <EOL> with h5py . File ( filename , '<STR_LIT:w>' ) as f : <EOL> f . create_dataset ( '<STR_LIT:X>' , data = dataset . get_design_matrix ( ) ) <EOL> f . create_dataset ( '<STR_LIT:y>' , data = dataset . get_targets ( ) ) <EOL> trainer = yaml_parse . load ( convert_to_one_hot_yaml % { '<STR_LIT:filename>' : filename } ) <EOL> trainer . main_loop ( ) <EOL> os . remove ( filename ) <EOL> def test_hdf5_load_all ( ) : <EOL> """<STR_LIT>""" <EOL> skip_if_no_h5py ( ) <EOL> import h5py <EOL> handle , filename = tempfile . mkstemp ( ) <EOL> dataset = random_one_hot_dense_design_matrix ( np . random . RandomState ( <NUM_LIT:1> ) , <EOL> num_examples = <NUM_LIT:10> , dim = <NUM_LIT:5> , <EOL> num_classes = <NUM_LIT:3> ) <EOL> with h5py . File ( filename , '<STR_LIT:w>' ) as f : <EOL> f . create_dataset ( '<STR_LIT:X>' , data = dataset . get_design_matrix ( ) ) <EOL> f . create_dataset ( '<STR_LIT:y>' , data = dataset . get_targets ( ) ) <EOL> trainer = yaml_parse . load ( load_all_yaml % { '<STR_LIT:filename>' : filename } ) <EOL> trainer . main_loop ( ) <EOL> os . remove ( filename ) <EOL> design_matrix_yaml = """<STR_LIT>""" <EOL> topo_view_yaml = """<STR_LIT>""" <EOL> convert_to_one_hot_yaml = """<STR_LIT>""" <EOL> load_all_yaml = """<STR_LIT>""" </s>
<s> """<STR_LIT>""" <EOL> __authors__ = "<STR_LIT>" <EOL> __copyright__ = "<STR_LIT>" <EOL> __credits__ = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" ] <EOL> __license__ = "<STR_LIT>" <EOL> __maintainer__ = "<STR_LIT>" <EOL> __email__ = "<STR_LIT>" <EOL> import functools <EOL> import numpy as np <EOL> from pylearn2 . datasets . dataset import Dataset <EOL> from pylearn2 . utils import wraps <EOL> from pylearn2 . utils . iteration import ( <EOL> FiniteDatasetIterator , <EOL> resolve_iterator_class <EOL> ) <EOL> from pylearn2 . utils . data_specs import is_flat_specs <EOL> from pylearn2 . utils . rng import make_np_rng <EOL> from pylearn2 . utils import contains_nan <EOL> class VectorSpacesDataset ( Dataset ) : <EOL> """<STR_LIT>""" <EOL> _default_seed = ( <NUM_LIT> , <NUM_LIT:2> , <NUM_LIT> ) <EOL> def __init__ ( self , data = None , data_specs = None , rng = _default_seed , <EOL> preprocessor = None , fit_preprocessor = False ) : <EOL> assert is_flat_specs ( data_specs ) <EOL> if isinstance ( data_specs [ <NUM_LIT:1> ] , tuple ) : <EOL> assert sorted ( set ( data_specs [ <NUM_LIT:1> ] ) ) == sorted ( data_specs [ <NUM_LIT:1> ] ) <EOL> space , source = data_specs <EOL> if isinstance ( data , list ) : <EOL> data = tuple ( data ) <EOL> space . np_validate ( data ) <EOL> assert len ( set ( elem . shape [ <NUM_LIT:0> ] for elem in list ( data ) ) ) <= <NUM_LIT:1> <EOL> self . data = data <EOL> self . data_specs = data_specs <EOL> self . num_examples = list ( data ) [ <NUM_LIT:0> ] . shape [ <NUM_LIT:0> ] <EOL> self . compress = False <EOL> self . design_loc = None <EOL> self . rng = make_np_rng ( rng , which_method = '<STR_LIT>' ) <EOL> self . _iter_mode = resolve_iterator_class ( '<STR_LIT>' ) <EOL> if preprocessor : <EOL> preprocessor . apply ( self , can_fit = fit_preprocessor ) <EOL> self . preprocessor = preprocessor <EOL> @ functools . wraps ( Dataset . iterator ) <EOL> def iterator ( self , mode = None , batch_size = None , num_batches = None , <EOL> rng = None , data_specs = None , <EOL> return_tuple = False ) : <EOL> if mode is None : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> mode = self . _iter_subset_class <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' % str ( self ) ) <EOL> else : <EOL> mode = resolve_iterator_class ( mode ) <EOL> if batch_size is None : <EOL> batch_size = getattr ( self , '<STR_LIT>' , None ) <EOL> if num_batches is None : <EOL> num_batches = getattr ( self , '<STR_LIT>' , None ) <EOL> if rng is None and mode . stochastic : <EOL> rng = self . rng <EOL> if data_specs is None : <EOL> data_specs = self . data_specs <EOL> return FiniteDatasetIterator ( <EOL> self , <EOL> mode ( self . get_num_examples ( ) , <EOL> batch_size , num_batches , rng ) , <EOL> data_specs = data_specs , return_tuple = return_tuple <EOL> ) <EOL> def get_data_specs ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . data_specs <EOL> def get_data ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . data <EOL> def set_data ( self , data , data_specs ) : <EOL> """<STR_LIT>""" <EOL> data_specs [ <NUM_LIT:0> ] . np_validate ( data ) <EOL> assert not [ contains_nan ( X ) for X in data ] <EOL> raise NotImplementedError ( ) <EOL> def get_source ( self , name ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> @ wraps ( Dataset . get_num_examples ) <EOL> def get_num_examples ( self ) : <EOL> return self . num_examples <EOL> def get_batch ( self , batch_size , data_specs = None ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> """<STR_LIT>""" </s>
<s> """<STR_LIT>""" <EOL> __author__ = "<STR_LIT>" <EOL> __copyright__ = "<STR_LIT>" <EOL> __credits__ = [ "<STR_LIT>" ] <EOL> __license__ = "<STR_LIT>" <EOL> __email__ = "<STR_LIT>" <EOL> __maintainer__ = "<STR_LIT>" <EOL> import numpy <EOL> def global_contrast_normalize ( X , scale = <NUM_LIT:1.> , subtract_mean = True , use_std = False , <EOL> sqrt_bias = <NUM_LIT:0.> , min_divisor = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> assert X . ndim == <NUM_LIT:2> , "<STR_LIT>" <EOL> scale = float ( scale ) <EOL> assert scale >= min_divisor <EOL> mean = X . mean ( axis = <NUM_LIT:1> ) <EOL> if subtract_mean : <EOL> X = X - mean [ : , numpy . newaxis ] <EOL> else : <EOL> X = X . copy ( ) <EOL> if use_std : <EOL> ddof = <NUM_LIT:1> <EOL> if X . shape [ <NUM_LIT:1> ] == <NUM_LIT:1> : <EOL> ddof = <NUM_LIT:0> <EOL> normalizers = numpy . sqrt ( sqrt_bias + X . var ( axis = <NUM_LIT:1> , ddof = ddof ) ) / scale <EOL> else : <EOL> normalizers = numpy . sqrt ( sqrt_bias + ( X ** <NUM_LIT:2> ) . sum ( axis = <NUM_LIT:1> ) ) / scale <EOL> normalizers [ normalizers < min_divisor ] = <NUM_LIT:1.> <EOL> X /= normalizers [ : , numpy . newaxis ] <EOL> return X </s>
<s> """<STR_LIT>""" <EOL> __author__ = "<STR_LIT>" <EOL> __license__ = "<STR_LIT>" <EOL> __credits__ = "<STR_LIT>" <EOL> __maintainer__ = "<STR_LIT>" <EOL> import theano <EOL> from theano import tensor <EOL> from theano . sandbox . cuda . dnn import dnn_available <EOL> from pylearn2 . linear . conv2d import Conv2D <EOL> from pylearn2 . linear . cudnn2d import Cudnn2D , make_random_conv2D <EOL> from pylearn2 . space import Conv2DSpace <EOL> from pylearn2 . utils import sharedX <EOL> from pylearn2 . testing . skip import skip_if_no_gpu <EOL> import unittest <EOL> from nose . plugins . skip import SkipTest <EOL> import numpy as np <EOL> class TestCudnn ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> skip_if_no_gpu ( ) <EOL> if not dnn_available ( ) : <EOL> raise SkipTest ( '<STR_LIT>' ) <EOL> self . orig_floatX = theano . config . floatX <EOL> theano . config . floatX = '<STR_LIT>' <EOL> self . image = np . random . rand ( <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:3> ) . astype ( theano . config . floatX ) <EOL> self . image_tensor = tensor . tensor4 ( ) <EOL> self . input_space = Conv2DSpace ( ( <NUM_LIT:3> , <NUM_LIT:3> ) , <NUM_LIT:1> , axes = ( '<STR_LIT:b>' , '<STR_LIT:c>' , <NUM_LIT:0> , <NUM_LIT:1> ) ) <EOL> self . filters_values = np . ones ( <EOL> ( <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:2> ) , dtype = theano . config . floatX <EOL> ) <EOL> self . filters = sharedX ( self . filters_values , name = '<STR_LIT>' ) <EOL> self . batch_size = <NUM_LIT:1> <EOL> self . cudnn2d = Cudnn2D ( self . filters , self . batch_size , self . input_space ) <EOL> def tearDown ( self ) : <EOL> """<STR_LIT>""" <EOL> theano . config . floatX = self . orig_floatX <EOL> def test_value_errors ( self ) : <EOL> """<STR_LIT>""" <EOL> with self . assertRaises ( AssertionError ) : <EOL> Cudnn2D ( filters = self . filters , batch_size = - <NUM_LIT:1> , <EOL> input_space = self . input_space ) <EOL> def test_get_params ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertEqual ( self . cudnn2d . get_params ( ) , [ self . filters ] ) <EOL> def test_get_weights_topo ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertTrue ( np . all ( <EOL> self . cudnn2d . get_weights_topo ( borrow = True ) == <EOL> np . transpose ( self . filters . get_value ( borrow = True ) , ( <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:1> ) ) ) ) <EOL> def test_lmul ( self ) : <EOL> """<STR_LIT>""" <EOL> conv2d = Conv2D ( self . filters , self . batch_size , self . input_space , <EOL> output_axes = ( '<STR_LIT:b>' , '<STR_LIT:c>' , <NUM_LIT:0> , <NUM_LIT:1> ) , ) <EOL> f_co = theano . function ( [ self . image_tensor ] , <EOL> conv2d . lmul ( self . image_tensor ) ) <EOL> f_cu = theano . function ( [ self . image_tensor ] , <EOL> self . cudnn2d . lmul ( self . image_tensor ) ) <EOL> self . assertTrue ( np . allclose ( f_co ( self . image ) , f_cu ( self . image ) ) ) <EOL> def test_set_batch_size ( self ) : <EOL> """<STR_LIT>""" <EOL> img_shape = self . cudnn2d . _img_shape <EOL> self . cudnn2d . set_batch_size ( self . batch_size + <NUM_LIT:10> ) <EOL> np . testing . assert_equal ( self . cudnn2d . _img_shape [ <NUM_LIT:0> ] , <EOL> self . batch_size + <NUM_LIT:10> ) <EOL> np . testing . assert_equal ( self . cudnn2d . _img_shape [ <NUM_LIT:1> : ] , img_shape [ <NUM_LIT:1> : ] ) <EOL> def test_axes ( self ) : <EOL> """<STR_LIT>""" <EOL> default_axes = ( '<STR_LIT:b>' , '<STR_LIT:c>' , <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> axes = ( <NUM_LIT:0> , '<STR_LIT:b>' , <NUM_LIT:1> , '<STR_LIT:c>' ) <EOL> another_axes = ( <NUM_LIT:0> , <NUM_LIT:1> , '<STR_LIT:c>' , '<STR_LIT:b>' ) <EOL> map_to_default = tuple ( axes . index ( axis ) for axis in default_axes ) <EOL> map_to_another_axes = tuple ( default_axes . index ( axis ) for <EOL> axis in another_axes ) <EOL> input_space = Conv2DSpace ( ( <NUM_LIT:3> , <NUM_LIT:3> ) , num_channels = <NUM_LIT:1> , axes = another_axes ) <EOL> cudnn2d = Cudnn2D ( self . filters , <NUM_LIT:1> , input_space , output_axes = axes ) <EOL> f = theano . function ( [ self . image_tensor ] , <EOL> cudnn2d . lmul ( self . image_tensor ) ) <EOL> f_def = theano . function ( [ self . image_tensor ] , <EOL> self . cudnn2d . lmul ( self . image_tensor ) ) <EOL> output = f ( np . transpose ( self . image , map_to_another_axes ) ) <EOL> output_def = np . array ( f_def ( self . image ) ) <EOL> output = np . transpose ( output , map_to_default ) <EOL> np . testing . assert_allclose ( output_def , output ) <EOL> np . testing . assert_equal ( output_def . shape , output . shape ) <EOL> def test_channels ( self ) : <EOL> """<STR_LIT>""" <EOL> input_space = Conv2DSpace ( ( <NUM_LIT:3> , <NUM_LIT:3> ) , num_channels = <NUM_LIT:3> ) <EOL> filters_values = np . ones ( <EOL> ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:2> , <NUM_LIT:2> ) , dtype = theano . config . floatX <EOL> ) <EOL> filters = sharedX ( filters_values ) <EOL> image = np . random . rand ( <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT:3> ) . astype ( theano . config . floatX ) <EOL> cudnn2d = Cudnn2D ( filters , <NUM_LIT:1> , input_space ) <EOL> f = theano . function ( [ self . image_tensor ] , <EOL> cudnn2d . lmul ( self . image_tensor ) ) <EOL> assert f ( image ) . shape == ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> ) <EOL> def test_make_random_conv2D ( self ) : <EOL> """<STR_LIT>""" <EOL> output_space = Conv2DSpace ( ( <NUM_LIT:2> , <NUM_LIT:2> ) , <NUM_LIT:1> ) <EOL> cudnn2d = make_random_conv2D ( <NUM_LIT:1> , self . input_space , output_space , <EOL> ( <NUM_LIT:2> , <NUM_LIT:2> ) , <NUM_LIT:1> ) <EOL> f = theano . function ( [ self . image_tensor ] , <EOL> cudnn2d . lmul ( self . image_tensor ) ) <EOL> assert f ( self . image ) . shape == ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:1> ) <EOL> assert cudnn2d . _input_space == self . input_space <EOL> assert cudnn2d . _output_axes == output_space . axes </s>
<s> """<STR_LIT>""" <EOL> import logging <EOL> import sys <EOL> import numpy <EOL> N = numpy <EOL> from theano . compat . six . moves import xrange <EOL> import warnings <EOL> from scipy import linalg , sparse <EOL> try : <EOL> from scipy . sparse . linalg import eigsh as eigen_symmetric <EOL> except ImportError : <EOL> try : <EOL> from scipy . sparse . linalg import eigen_symmetric <EOL> except ImportError : <EOL> try : <EOL> from scipy . sparse . linalg . eigen . arpack import eigen_symmetric <EOL> except ImportError : <EOL> warnings . warn ( '<STR_LIT>' '<STR_LIT>' ) <EOL> from scipy . sparse . csr import csr_matrix <EOL> import theano <EOL> from theano import tensor <EOL> from theano . sparse import SparseType , structured_dot <EOL> from scipy import linalg <EOL> from scipy . sparse . csr import csr_matrix <EOL> try : <EOL> from scipy . sparse . linalg import eigen_symmetric <EOL> except ImportError : <EOL> try : <EOL> from scipy . sparse . linalg import eigsh as eigen_symmetric <EOL> except ImportError : <EOL> warnings . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> warnings . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> from pylearn2 . blocks import Block <EOL> from pylearn2 . utils import sharedX <EOL> logger = logging . getLogger ( ) <EOL> class _PCABase ( Block ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , num_components = None , min_variance = <NUM_LIT:0.0> , whiten = False ) : <EOL> super ( _PCABase , self ) . __init__ ( ) <EOL> self . num_components = num_components <EOL> self . min_variance = min_variance <EOL> self . whiten = whiten <EOL> self . W = None <EOL> self . v = None <EOL> self . mean = None <EOL> self . component_cutoff = theano . shared ( <EOL> theano . _asarray ( <NUM_LIT:0> , dtype = '<STR_LIT>' ) , <EOL> name = '<STR_LIT>' ) <EOL> self . _params = [ ] <EOL> def train ( self , X , mean = None ) : <EOL> """<STR_LIT>""" <EOL> if self . num_components is None : <EOL> self . num_components = X . shape [ <NUM_LIT:1> ] <EOL> if mean is None : <EOL> mean = X . mean ( axis = <NUM_LIT:0> ) <EOL> X = X - mean <EOL> v , W = self . _cov_eigen ( X ) <EOL> self . W = sharedX ( W , name = '<STR_LIT>' ) <EOL> self . v = sharedX ( v , name = '<STR_LIT:v>' ) <EOL> self . mean = sharedX ( mean , name = '<STR_LIT>' ) <EOL> self . _update_cutoff ( ) <EOL> component_cutoff = self . component_cutoff . get_value ( borrow = True ) <EOL> self . v . set_value ( self . v . get_value ( borrow = True ) [ : component_cutoff ] ) <EOL> self . W . set_value ( self . W . get_value ( borrow = True ) [ : , : component_cutoff ] ) <EOL> def __call__ ( self , inputs ) : <EOL> """<STR_LIT>""" <EOL> self . _update_cutoff ( ) <EOL> normalized_mean = inputs - self . mean <EOL> normalized_mean . name = '<STR_LIT>' <EOL> W = self . W [ : , : self . component_cutoff ] <EOL> if self . whiten : <EOL> W = W / tensor . sqrt ( self . v [ : self . component_cutoff ] ) <EOL> Y = tensor . dot ( normalized_mean , W ) <EOL> return Y <EOL> def get_weights ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _update_cutoff ( ) <EOL> component_cutoff = self . component_cutoff . get_value ( ) <EOL> W = self . W . get_value ( borrow = False ) <EOL> W = W [ : , : component_cutoff ] <EOL> if self . whiten : <EOL> W /= N . sqrt ( self . v . get_value ( borrow = False ) [ : component_cutoff ] ) <EOL> return W <EOL> def reconstruct ( self , inputs , add_mean = True ) : <EOL> """<STR_LIT>""" <EOL> self . _update_cutoff ( ) <EOL> if self . whiten : <EOL> inputs *= tensor . sqrt ( self . v [ : self . component_cutoff ] ) <EOL> X = tensor . dot ( inputs , self . W [ : , : self . component_cutoff ] . T ) <EOL> if add_mean : <EOL> X = X + self . mean <EOL> return X <EOL> def _update_cutoff ( self ) : <EOL> """<STR_LIT>""" <EOL> assert self . num_components is not None and self . num_components > <NUM_LIT:0> , '<STR_LIT>' <EOL> v = self . v . get_value ( borrow = True ) <EOL> var_mask = v / v . sum ( ) > self . min_variance <EOL> assert numpy . any ( var_mask ) , '<STR_LIT>' <EOL> var_cutoff = <NUM_LIT:1> + numpy . where ( var_mask ) [ <NUM_LIT:0> ] . max ( ) <EOL> self . component_cutoff . set_value ( min ( var_cutoff , self . num_components ) ) <EOL> def _cov_eigen ( self , X ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( '<STR_LIT>' + <EOL> '<STR_LIT>' ) <EOL> class SparseMatPCA ( _PCABase ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , batch_size = <NUM_LIT:50> , ** kwargs ) : <EOL> super ( SparseMatPCA , self ) . __init__ ( ** kwargs ) <EOL> self . minibatch_size = batch_size <EOL> def get_input_type ( self ) : <EOL> """<STR_LIT>""" <EOL> return csr_matrix <EOL> def _cov_eigen ( self , X ) : <EOL> """<STR_LIT>""" <EOL> n , d = X . shape <EOL> cov = numpy . zeros ( ( d , d ) ) <EOL> batch_size = self . minibatch_size <EOL> for i in xrange ( <NUM_LIT:0> , n , batch_size ) : <EOL> logger . info ( '<STR_LIT>' . format ( i ) ) <EOL> end = min ( n , i + batch_size ) <EOL> x = X [ i : end , : ] . todense ( ) - self . mean_ <EOL> assert x . shape [ <NUM_LIT:0> ] == end - i <EOL> prod = numpy . dot ( x . T , x ) <EOL> assert prod . shape == ( d , d ) <EOL> cov += prod <EOL> cov /= n <EOL> logger . info ( '<STR_LIT>' ) <EOL> v , W = linalg . eigh ( cov , eigvals = ( d - self . num_components , d - <NUM_LIT:1> ) ) <EOL> v , W = v [ : : - <NUM_LIT:1> ] , W [ : , : : - <NUM_LIT:1> ] <EOL> return v , W <EOL> def train ( self , X ) : <EOL> """<STR_LIT>""" <EOL> assert sparse . issparse ( X ) <EOL> logger . info ( '<STR_LIT>' ) <EOL> self . mean_ = numpy . asarray ( X . mean ( axis = <NUM_LIT:0> ) ) [ <NUM_LIT:0> , : ] <EOL> super ( SparseMatPCA , self ) . train ( X , mean = self . mean_ ) <EOL> def __call__ ( self , inputs ) : <EOL> """<STR_LIT>""" <EOL> self . _update_cutoff ( ) <EOL> Y = structured_dot ( inputs , self . W [ : , : self . component_cutoff ] ) <EOL> Z = Y - tensor . dot ( self . mean , self . W [ : , : self . component_cutoff ] ) <EOL> if self . whiten : <EOL> Z /= tensor . sqrt ( self . v [ : self . component_cutoff ] ) <EOL> return Z <EOL> def function ( self , name = None ) : <EOL> """<STR_LIT>""" <EOL> inputs = SparseType ( '<STR_LIT>' , dtype = theano . config . floatX ) ( ) <EOL> return theano . function ( [ inputs ] , self ( inputs ) , name = name ) <EOL> class OnlinePCA ( _PCABase ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , minibatch_size = <NUM_LIT> , ** kwargs ) : <EOL> super ( OnlinePCA , self ) . __init__ ( ** kwargs ) <EOL> self . minibatch_size = minibatch_size <EOL> def _cov_eigen ( self , X ) : <EOL> """<STR_LIT>""" <EOL> num_components = min ( self . num_components , X . shape [ <NUM_LIT:1> ] ) <EOL> pca_estimator = PcaOnlineEstimator ( X . shape [ <NUM_LIT:1> ] , <EOL> n_eigen = num_components , <EOL> minibatch_size = self . minibatch_size , <EOL> centering = False <EOL> ) <EOL> logger . debug ( '<STR_LIT:*>' * <NUM_LIT:50> ) <EOL> for i in range ( X . shape [ <NUM_LIT:0> ] ) : <EOL> if ( i + <NUM_LIT:1> ) % ( X . shape [ <NUM_LIT:0> ] / <NUM_LIT:50> ) == <NUM_LIT:0> : <EOL> logger . debug ( '<STR_LIT:|>' ) <EOL> pca_estimator . observe ( X [ i , : ] ) <EOL> v , W = pca_estimator . getLeadingEigen ( ) <EOL> return v [ : : - <NUM_LIT:1> ] , W . T [ : , : : - <NUM_LIT:1> ] <EOL> class Cov : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , batch_size ) : <EOL> self . batch_size = batch_size <EOL> def __call__ ( self , X ) : <EOL> """<STR_LIT>""" <EOL> X = X . T <EOL> m , n = X . shape <EOL> mean = X . mean ( axis = <NUM_LIT:0> ) <EOL> rval = N . zeros ( ( n , n ) ) <EOL> for i in xrange ( <NUM_LIT:0> , m , self . batch_size ) : <EOL> B = X [ i : i + self . batch_size , : ] - mean <EOL> rval += N . dot ( B . T , B ) <EOL> return rval / float ( m - <NUM_LIT:1> ) <EOL> class CovEigPCA ( _PCABase ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , cov_batch_size = None , ** kwargs ) : <EOL> super ( CovEigPCA , self ) . __init__ ( ** kwargs ) <EOL> if cov_batch_size is not None : <EOL> self . cov = Cov ( cov_batch_size ) <EOL> else : <EOL> self . cov = numpy . cov <EOL> def _cov_eigen ( self , X ) : <EOL> """<STR_LIT>""" <EOL> v , W = linalg . eigh ( self . cov ( X . T ) ) <EOL> return v [ : : - <NUM_LIT:1> ] , W [ : , : : - <NUM_LIT:1> ] <EOL> class SVDPCA ( _PCABase ) : <EOL> """<STR_LIT>""" <EOL> def _cov_eigen ( self , X ) : <EOL> """<STR_LIT>""" <EOL> U , s , Vh = linalg . svd ( X , full_matrices = False ) <EOL> return s ** <NUM_LIT:2> , Vh . T <EOL> class SparsePCA ( _PCABase ) : <EOL> """<STR_LIT>""" <EOL> def train ( self , X , mean = None ) : <EOL> """<STR_LIT>""" <EOL> warnings . warn ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> n , d = X . shape <EOL> mean = X . mean ( axis = <NUM_LIT:0> ) <EOL> mean_matrix = csr_matrix ( mean . repeat ( n ) . reshape ( ( d , n ) ) ) . T <EOL> X = X - mean_matrix <EOL> super ( SparsePCA , self ) . train ( X , mean = numpy . asarray ( mean ) . squeeze ( ) ) <EOL> def _cov_eigen ( self , X ) : <EOL> """<STR_LIT>""" <EOL> v , W = eigen_symmetric ( X . T . dot ( X ) / X . shape [ <NUM_LIT:0> ] , k = self . num_components ) <EOL> return v [ : : - <NUM_LIT:1> ] , W [ : , : : - <NUM_LIT:1> ] <EOL> def __call__ ( self , inputs ) : <EOL> """<STR_LIT>""" <EOL> self . _update_cutoff ( ) <EOL> Y = structured_dot ( inputs , self . W [ : , : self . component_cutoff ] ) <EOL> if self . whiten : <EOL> Y /= tensor . sqrt ( self . v [ : self . component_cutoff ] ) <EOL> return Y <EOL> def function ( self , name = None ) : <EOL> """<STR_LIT>""" <EOL> inputs = SparseType ( '<STR_LIT>' , dtype = theano . config . floatX ) ( ) <EOL> return theano . function ( [ inputs ] , self ( inputs ) , name = name ) <EOL> class PcaOnlineEstimator ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , n_dim , n_eigen = <NUM_LIT:10> , minibatch_size = <NUM_LIT> , gamma = <NUM_LIT> , <EOL> regularizer = <NUM_LIT> , centering = True ) : <EOL> self . n_dim = n_dim <EOL> self . n_eigen = n_eigen <EOL> self . minibatch_size = minibatch_size <EOL> self . gamma = gamma <EOL> self . regularizer = regularizer <EOL> self . centering = centering <EOL> self . n_observations = <NUM_LIT:0> <EOL> self . minibatch_index = <NUM_LIT:0> <EOL> self . Xt = numpy . zeros ( [ self . n_eigen + self . minibatch_size , self . n_dim ] ) <EOL> self . x_sum = numpy . zeros ( [ self . n_dim ] ) <EOL> self . G = numpy . zeros ( [ self . n_eigen + self . minibatch_size , <EOL> self . n_eigen + self . minibatch_size ] ) <EOL> for i in range ( self . n_eigen ) : <EOL> self . G [ i , i ] = self . regularizer <EOL> self . d = numpy . zeros ( [ self . n_eigen + self . minibatch_size ] ) <EOL> self . V = numpy . zeros ( [ self . n_eigen + self . minibatch_size , <EOL> self . n_eigen + self . minibatch_size ] ) <EOL> self . Ut = numpy . zeros ( [ self . n_eigen , self . n_dim ] ) <EOL> def observe ( self , x ) : <EOL> """<STR_LIT>""" <EOL> assert ( numpy . size ( x ) == self . n_dim ) <EOL> self . n_observations += <NUM_LIT:1> <EOL> row = self . n_eigen + self . minibatch_index <EOL> self . Xt [ row ] = x <EOL> self . x_sum *= self . gamma <EOL> self . x_sum += x <EOL> normalizer = ( <NUM_LIT:1.0> - pow ( self . gamma , self . n_observations ) ) / ( <NUM_LIT:1.0> - self . gamma ) <EOL> if self . centering : <EOL> self . Xt [ row ] -= self . x_sum / normalizer <EOL> rn = pow ( self . gamma , - <NUM_LIT:0.5> * ( self . minibatch_index + <NUM_LIT:1> ) ) ; <EOL> self . Xt [ row ] *= rn <EOL> self . G [ : row + <NUM_LIT:1> , row ] = numpy . dot ( self . Xt [ : row + <NUM_LIT:1> , : ] , <EOL> self . Xt [ row , : ] . transpose ( ) ) <EOL> self . G [ row , : row ] = self . G [ : row , row ] . transpose ( ) <EOL> self . minibatch_index += <NUM_LIT:1> <EOL> if self . minibatch_index == self . minibatch_size : <EOL> self . reevaluate ( ) <EOL> def reevaluate ( self ) : <EOL> """<STR_LIT>""" <EOL> assert ( self . minibatch_index == self . minibatch_size ) ; <EOL> for i in range ( self . n_eigen + self . minibatch_size ) : <EOL> self . G [ i , i ] += self . regularizer <EOL> self . d , self . V = linalg . eigh ( self . G ) <EOL> self . Ut = numpy . dot ( self . V [ : , - self . n_eigen : ] . transpose ( ) , self . Xt ) <EOL> rn = pow ( self . gamma , - <NUM_LIT:0.5> * ( self . minibatch_index + <NUM_LIT:1> ) ) <EOL> inv_rn2 = <NUM_LIT:1.0> / ( rn * rn ) <EOL> self . Ut *= <NUM_LIT:1.0> / rn <EOL> self . d *= inv_rn2 ; <EOL> self . Xt [ : self . n_eigen , : ] = self . Ut <EOL> for i in range ( self . n_eigen ) : <EOL> self . G [ i , i ] = self . d [ - self . n_eigen + i ] <EOL> self . minibatch_index = <NUM_LIT:0> <EOL> def getLeadingEigen ( self ) : <EOL> """<STR_LIT>""" <EOL> normalizer = ( <NUM_LIT:1.0> - pow ( self . gamma , <EOL> self . n_observations - self . minibatch_index ) ) / ( <NUM_LIT:1.0> - self . gamma ) <EOL> eigvals = self . d [ - self . n_eigen : ] / normalizer <EOL> eigvecs = numpy . zeros ( [ self . n_eigen , self . n_dim ] ) <EOL> for i in range ( self . n_eigen ) : <EOL> eigvecs [ i ] = self . Ut [ - self . n_eigen + i ] / numpy . sqrt ( numpy . dot ( self . Ut [ - self . n_eigen + i ] , <EOL> self . Ut [ - self . n_eigen + i ] ) ) <EOL> return [ eigvals , eigvecs ] <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> """<STR_LIT>""" <EOL> import argparse <EOL> from pylearn2 . utils import load_data <EOL> parser = argparse . ArgumentParser ( <EOL> description = "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store>' , <EOL> type = str , <EOL> choices = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store>' , <EOL> type = str , <EOL> default = None , <EOL> required = False , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store>' , <EOL> type = str , <EOL> default = '<STR_LIT>' , <EOL> required = False , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store>' , <EOL> type = str , <EOL> choices = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> default = '<STR_LIT>' , <EOL> required = False , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store>' , <EOL> type = int , <EOL> default = <NUM_LIT> , <EOL> required = False , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store>' , <EOL> type = int , <EOL> default = None , <EOL> required = False , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store>' , <EOL> type = float , <EOL> default = <NUM_LIT:0.0> , <EOL> required = False , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT>' , <EOL> default = False , <EOL> const = True , <EOL> required = False , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> args = parser . parse_args ( ) <EOL> data = load_data ( { '<STR_LIT>' : args . dataset } ) <EOL> train_data , valid_data , test_data = map ( lambda x : <EOL> x . get_value ( borrow = True ) , data ) <EOL> logger . info ( "<STR_LIT>" . format ( map ( lambda x : <EOL> x . get_value ( ) . shape , data ) ) ) <EOL> conf = { <EOL> '<STR_LIT>' : args . num_components , <EOL> '<STR_LIT>' : args . min_variance , <EOL> '<STR_LIT>' : args . whiten <EOL> } <EOL> if args . algorithm == '<STR_LIT>' : <EOL> PCAImpl = CovEigPCA <EOL> elif args . algorithm == '<STR_LIT>' : <EOL> PCAImpl = SVDPCA <EOL> elif args . algorithm == '<STR_LIT>' : <EOL> PCAImpl = OnlinePCA <EOL> conf [ '<STR_LIT>' ] = args . minibatch_size <EOL> else : <EOL> raise NotImplementedError ( args . algorithm ) <EOL> if args . load_file : <EOL> pca = Block . load ( args . load_file ) <EOL> else : <EOL> logger . info ( "<STR_LIT>" ) <EOL> pca = PCAImpl ( ** conf ) <EOL> pca . train ( train_data ) <EOL> pca . save ( args . save_file ) <EOL> inputs = tensor . matrix ( ) <EOL> pca_transform = theano . function ( [ inputs ] , pca ( inputs ) ) <EOL> valid_pca = pca_transform ( valid_data ) <EOL> test_pca = pca_transform ( test_data ) <EOL> logger . info ( "<STR_LIT>" . format ( map ( numpy . shape , <EOL> [ valid_pca , test_pca ] ) ) ) </s>
<s> """<STR_LIT>""" <EOL> __authors__ = "<STR_LIT>" <EOL> __copyright__ = "<STR_LIT>" <EOL> __credits__ = [ "<STR_LIT>" ] <EOL> __license__ = "<STR_LIT>" <EOL> __maintainer__ = "<STR_LIT>" <EOL> import numpy as np <EOL> import theano <EOL> from pylearn2 . models . mlp import MLP , WindowLayer <EOL> from pylearn2 . space import Conv2DSpace <EOL> def build_mlp_fn ( x0 , y0 , x1 , y1 , s0 , s1 , c , axes ) : <EOL> """<STR_LIT>""" <EOL> mlp = MLP ( layers = [ WindowLayer ( '<STR_LIT>' , window = ( x0 , y0 , x1 , y1 ) ) ] , <EOL> input_space = Conv2DSpace ( shape = ( s0 , s1 ) , <EOL> num_channels = c , axes = axes ) ) <EOL> X = mlp . get_input_space ( ) . make_batch_theano ( ) <EOL> f = theano . function ( [ X ] , mlp . fprop ( X ) ) <EOL> return f <EOL> def test_windowlayer ( ) : <EOL> """<STR_LIT>""" <EOL> np . testing . assert_raises ( ValueError , build_mlp_fn , <EOL> <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:20> , <NUM_LIT:20> , <NUM_LIT:20> , <NUM_LIT:20> , <NUM_LIT:3> , ( '<STR_LIT:c>' , <NUM_LIT:0> , <NUM_LIT:1> , '<STR_LIT:b>' ) ) <EOL> np . testing . assert_raises ( ValueError , build_mlp_fn , <EOL> - <NUM_LIT:1> , - <NUM_LIT:1> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:20> , <NUM_LIT:20> , <NUM_LIT:3> , ( '<STR_LIT:c>' , <NUM_LIT:0> , <NUM_LIT:1> , '<STR_LIT:b>' ) ) <EOL> fprop = build_mlp_fn ( <NUM_LIT:5> , <NUM_LIT:5> , <NUM_LIT:10> , <NUM_LIT:15> , <NUM_LIT:20> , <NUM_LIT:20> , <NUM_LIT:2> , ( '<STR_LIT:b>' , '<STR_LIT:c>' , <NUM_LIT:0> , <NUM_LIT:1> ) ) <EOL> n = np . random . rand ( <NUM_LIT:3> , <NUM_LIT:2> , <NUM_LIT:20> , <NUM_LIT:20> ) . astype ( theano . config . floatX ) <EOL> r = fprop ( n ) <EOL> assert r . shape == ( <NUM_LIT:3> , <NUM_LIT:2> , <NUM_LIT:6> , <NUM_LIT:11> ) <EOL> assert r [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] == n [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:5> , <NUM_LIT:5> ] <EOL> assert r [ <NUM_LIT:2> , <NUM_LIT:1> , <NUM_LIT:5> , <NUM_LIT:10> ] == n [ <NUM_LIT:2> , <NUM_LIT:1> , <NUM_LIT:10> , <NUM_LIT:15> ] <EOL> assert r [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:3> ] == n [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:8> , <NUM_LIT:8> ] <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> test_windowlayer ( ) </s>
<s> from theano . compat . six . moves import xrange <EOL> activate_test_spconv = <NUM_LIT:0> <EOL> if activate_test_spconv : <EOL> import sys <EOL> from theano import function , Mode <EOL> from theano . gof import OpWiseCLinker <EOL> import theano , numpy <EOL> import theano . tensor as T <EOL> import theano . sparse <EOL> import scipy . sparse <EOL> from scipy . signal import convolve2d <EOL> import scipy . sparse as sparse <EOL> import numpy <EOL> import numpy as N <EOL> import unittest <EOL> import time <EOL> sp = None <EOL> def test_convolution ( ) : <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' ) <EOL> channels = <NUM_LIT:3> <EOL> bsize = <NUM_LIT:10> <EOL> imshp = ( <NUM_LIT:32> , <NUM_LIT:32> ) <EOL> kshp = ( <NUM_LIT:8> , <NUM_LIT:8> ) <EOL> nkern = <NUM_LIT:32> <EOL> subsample_amounts = ( ( <NUM_LIT:1> , <NUM_LIT:1> ) , ( <NUM_LIT:2> , <NUM_LIT:2> ) , ( <NUM_LIT:3> , <NUM_LIT:3> ) , ( <NUM_LIT:4> , <NUM_LIT:4> ) ) <EOL> convmodes = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> ishp4_channel_major = ( bsize , channels ) + imshp <EOL> kshp4_channel_major = ( nkern , channels ) + kshp <EOL> ishp4_channel_minor = ( bsize , ) + imshp + ( channels , ) <EOL> kshp4_channel_minor = ( nkern , ) + kshp + ( channels , ) <EOL> kerns = T . tensor4 ( ) <EOL> imgs = T . tensor4 ( ) <EOL> rng = N . random . RandomState ( <NUM_LIT> ) <EOL> kern_data = rng . rand ( * kshp4_channel_major ) . astype ( kerns . dtype ) + <NUM_LIT:1> <EOL> img_data = rng . rand ( * ishp4_channel_major ) . astype ( imgs . dtype ) + <NUM_LIT:1> <EOL> kern_data_minor = kern_data . transpose ( [ <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:1> ] ) . copy ( ) <EOL> img_data_minor = img_data . transpose ( [ <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:1> ] ) . copy ( ) <EOL> assert img_data_minor . shape == ( bsize , ) + imshp + ( channels , ) <EOL> for conv_mode in convmodes : <EOL> for subsample in subsample_amounts : <EOL> de_output = theano . tensor . nnet . conv2d ( imgs , kerns , <EOL> ishp4_channel_major , <EOL> kshp4_channel_major , <EOL> border_mode = conv_mode , <EOL> subsample = subsample ) <EOL> f_d = function ( [ kerns , imgs ] , de_output , profile = '<STR_LIT>' ) <EOL> t0 = time . time ( ) <EOL> for i in range ( <NUM_LIT:5> ) : <EOL> rval_d = f_d ( kern_data , img_data ) <EOL> t_d = time . time ( ) - t0 <EOL> use_channel_major_ordering = <NUM_LIT:0> <EOL> if use_channel_major_ordering : <EOL> sp_output , outshp = sp . conv2d ( imgs , kerns , <EOL> ishp4_channel_major , <EOL> kshp4_channel_major , <EOL> subsample = subsample , <EOL> border_mode = conv_mode ) <EOL> f_s = function ( [ kerns , imgs ] , sp_output , <EOL> profile = '<STR_LIT>' ) <EOL> t0 = time . time ( ) <EOL> for i in range ( <NUM_LIT:5> ) : <EOL> rval_s = f_s ( kern_data , img_data ) <EOL> assert rval_s . size == rval_d . size , ( rval_s . shape , rval_d . shape ) <EOL> rval_s_major = rval_s . transpose ( [ <NUM_LIT:0> , <NUM_LIT:3> , <NUM_LIT:1> , <NUM_LIT:2> ] ) <EOL> assert numpy . allclose ( rval_s_major , rval_d ) <EOL> t_s_major = time . time ( ) - t0 <EOL> use_channel_minor_ordering = <NUM_LIT:1> <EOL> if use_channel_minor_ordering : <EOL> sp_output , outshp = sp . conv2d_channel_minor ( imgs , kerns , <EOL> ishp4_channel_minor , <EOL> kshp4_channel_minor , <EOL> subsample = subsample , <EOL> border_mode = conv_mode ) <EOL> f_s = function ( [ kerns , imgs ] , sp_output , <EOL> profile = '<STR_LIT>' ) <EOL> t0 = time . time ( ) <EOL> for i in range ( <NUM_LIT:5> ) : <EOL> rval_s = f_s ( kern_data_minor , img_data_minor ) <EOL> assert rval_s . size == rval_d . size , ( rval_s . shape , rval_d . shape ) <EOL> rval_s_major = rval_s . transpose ( [ <NUM_LIT:0> , <NUM_LIT:3> , <NUM_LIT:1> , <NUM_LIT:2> ] ) <EOL> assert rval_s_major . shape == rval_d . shape <EOL> assert numpy . allclose ( rval_s_major , rval_d ) <EOL> t_s_minor = time . time ( ) - t0 <EOL> def test_sparse ( ) : <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' ) <EOL> bsize = <NUM_LIT:10> <EOL> imshp = ( <NUM_LIT> , <NUM_LIT> ) <EOL> kshp = ( <NUM_LIT:5> , <NUM_LIT:5> ) <EOL> nkern = <NUM_LIT:1> <EOL> ssizes = ( ( <NUM_LIT:1> , <NUM_LIT:1> ) , ( <NUM_LIT:2> , <NUM_LIT:2> ) ) <EOL> convmodes = ( '<STR_LIT>' , '<STR_LIT>' , ) <EOL> bias = T . dvector ( ) <EOL> kerns = T . dvector ( ) <EOL> input = T . dmatrix ( ) <EOL> rng = N . random . RandomState ( <NUM_LIT> ) <EOL> import theano . gof as gof <EOL> ntot , ttot = <NUM_LIT:0> , <NUM_LIT:0> <EOL> for conv_mode in convmodes : <EOL> for ss in ssizes : <EOL> output , outshp = sp . applySparseFilter ( kerns , kshp , nkern , input , imshp , ss , bias = bias , mode = conv_mode ) <EOL> f = function ( [ kerns , bias , input ] , output ) <EOL> img2d = N . arange ( bsize * N . prod ( imshp ) ) . reshape ( ( bsize , ) + imshp ) <EOL> img1d = img2d . reshape ( bsize , - <NUM_LIT:1> ) <EOL> zeropad_img = N . zeros ( ( bsize , img2d . shape [ <NUM_LIT:1> ] + <NUM_LIT:2> * ( kshp [ <NUM_LIT:0> ] - <NUM_LIT:1> ) , img2d . shape [ <NUM_LIT:2> ] + <NUM_LIT:2> * ( kshp [ <NUM_LIT:1> ] - <NUM_LIT:1> ) ) ) <EOL> zeropad_img [ : , kshp [ <NUM_LIT:0> ] - <NUM_LIT:1> : kshp [ <NUM_LIT:0> ] - <NUM_LIT:1> + img2d . shape [ <NUM_LIT:1> ] , <EOL> kshp [ <NUM_LIT:1> ] - <NUM_LIT:1> : kshp [ <NUM_LIT:1> ] - <NUM_LIT:1> + img2d . shape [ <NUM_LIT:2> ] ] = img2d <EOL> filters = N . arange ( N . prod ( outshp ) * N . prod ( kshp ) ) . reshape ( nkern , N . prod ( outshp [ <NUM_LIT:1> : ] ) , N . prod ( kshp ) ) <EOL> spfilt = filters . flatten ( ) <EOL> biasvals = N . arange ( N . prod ( outshp ) ) <EOL> ntime1 = time . time ( ) <EOL> refout = N . zeros ( ( bsize , nkern , outshp [ <NUM_LIT:1> ] , outshp [ <NUM_LIT:2> ] ) ) <EOL> patch = N . zeros ( ( kshp [ <NUM_LIT:0> ] , kshp [ <NUM_LIT:1> ] ) ) <EOL> for b in xrange ( bsize ) : <EOL> for k in xrange ( nkern ) : <EOL> pixi = <NUM_LIT:0> <EOL> for j in xrange ( outshp [ <NUM_LIT:1> ] ) : <EOL> for i in xrange ( outshp [ <NUM_LIT:2> ] ) : <EOL> n = j * ss [ <NUM_LIT:0> ] <EOL> m = i * ss [ <NUM_LIT:1> ] <EOL> patch = zeropad_img [ b , n : n + kshp [ <NUM_LIT:0> ] , m : m + kshp [ <NUM_LIT:1> ] ] <EOL> refout [ b , k , j , i ] = N . dot ( filters [ k , pixi , : ] , patch . flatten ( ) ) <EOL> pixi += <NUM_LIT:1> <EOL> refout = refout . reshape ( bsize , - <NUM_LIT:1> ) + biasvals <EOL> ntot += time . time ( ) - ntime1 <EOL> ttime1 = time . time ( ) <EOL> out1 = f ( spfilt , biasvals , img1d ) <EOL> ttot += time . time ( ) - ttime1 <EOL> temp = refout - out1 <EOL> assert ( temp < <NUM_LIT> ) . all ( ) <EOL> vis = T . grad ( output , input , output ) <EOL> downprop = function ( [ kerns , output ] , vis ) <EOL> temp1 = time . time ( ) <EOL> for zz in range ( <NUM_LIT:100> ) : <EOL> visval = downprop ( spfilt , out1 ) <EOL> indices , indptr , spmat_shape , sptype , outshp , kmap = sp . convolution_indices . sparse_eval ( imshp , kshp , nkern , ss , conv_mode ) <EOL> spmat = sparse . csc_matrix ( ( spfilt [ kmap ] , indices , indptr ) , spmat_shape ) <EOL> visref = N . dot ( out1 , spmat . todense ( ) ) <EOL> assert N . all ( visref == visval ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' , ntot ) <EOL> print ( '<STR_LIT>' , ttot ) <EOL> def test_maxpool ( ) : <EOL> maxpoolshps = ( ( <NUM_LIT:2> , <NUM_LIT:2> ) , ( <NUM_LIT:3> , <NUM_LIT:3> ) , ( <NUM_LIT:4> , <NUM_LIT:4> ) , ( <NUM_LIT:5> , <NUM_LIT:5> ) , ( <NUM_LIT:6> , <NUM_LIT:6> ) ) <EOL> imval = N . random . rand ( <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:10> , <NUM_LIT:10> ) <EOL> images = T . dmatrix ( ) <EOL> for maxpoolshp in maxpoolshps : <EOL> output , outshp = sp . max_pool ( images , imval . shape [ <NUM_LIT:1> : ] , maxpoolshp ) <EOL> f = function ( [ images , ] , [ output , ] ) <EOL> output_val = f ( imval . reshape ( imval . shape [ <NUM_LIT:0> ] , - <NUM_LIT:1> ) ) <EOL> my_output_val = N . zeros ( ( imval . shape [ <NUM_LIT:0> ] , imval . shape [ <NUM_LIT:1> ] , <EOL> imval . shape [ <NUM_LIT:2> ] / maxpoolshp [ <NUM_LIT:0> ] , <EOL> imval . shape [ <NUM_LIT:3> ] / maxpoolshp [ <NUM_LIT:1> ] ) ) <EOL> assert N . prod ( my_output_val . shape [ <NUM_LIT:1> : ] ) == N . prod ( N . r_ [ imval . shape [ <NUM_LIT:1> ] , outshp ] ) <EOL> for n in range ( imval . shape [ <NUM_LIT:0> ] ) : <EOL> for k in range ( imval . shape [ <NUM_LIT:1> ] ) : <EOL> for i in range ( imval . shape [ <NUM_LIT:2> ] / maxpoolshp [ <NUM_LIT:0> ] ) : <EOL> for j in range ( imval . shape [ <NUM_LIT:3> ] / maxpoolshp [ <NUM_LIT:1> ] ) : <EOL> ii , jj = i * maxpoolshp [ <NUM_LIT:0> ] , j * maxpoolshp [ <NUM_LIT:1> ] <EOL> patch = imval [ n , k , ii : ii + maxpoolshp [ <NUM_LIT:0> ] , jj : jj + maxpoolshp [ <NUM_LIT:1> ] ] <EOL> my_output_val [ n , k , i , j ] = N . max ( patch ) <EOL> my_output_val = my_output_val . reshape ( imval . shape [ <NUM_LIT:0> ] , - <NUM_LIT:1> ) <EOL> assert N . all ( output_val == my_output_val ) <EOL> def mp ( input ) : <EOL> output , outshp = sp . max_pool ( input , imval . shape [ <NUM_LIT:1> : ] , maxpoolshp ) <EOL> return output <EOL> T . verify_grad ( None , mp , [ imval . reshape ( imval . shape [ <NUM_LIT:0> ] , - <NUM_LIT:1> ) ] ) </s>
<s> from __future__ import print_function <EOL> __authors__ = "<STR_LIT>" <EOL> from pylearn2 . testing . skip import skip_if_no_gpu <EOL> skip_if_no_gpu ( ) <EOL> import numpy as np <EOL> from theano . compat . six . moves import xrange <EOL> from theano import shared <EOL> from theano . tensor import grad , constant <EOL> from pylearn2 . sandbox . cuda_convnet . filter_acts import FilterActs <EOL> from pylearn2 . sandbox . cuda_convnet . filter_acts import ImageActs <EOL> from theano . sandbox . cuda import gpu_from_host <EOL> from theano . sandbox . cuda import host_from_gpu <EOL> from theano . sandbox . rng_mrg import MRG_RandomStreams <EOL> from theano . tensor . nnet . conv import conv2d <EOL> from theano . tensor import as_tensor_variable <EOL> from theano import function <EOL> from theano import tensor as T <EOL> import warnings <EOL> from theano . sandbox import cuda <EOL> from theano . sandbox . cuda . var import float32_shared_constructor <EOL> from test_filter_acts_strided import FilterActs_python <EOL> def ImageActs_python ( filters , <EOL> hidacts , <EOL> stride = <NUM_LIT:1> , <EOL> img_shape = None , <EOL> ) : <EOL> if int ( stride ) != stride : <EOL> raise TypeError ( '<STR_LIT>' , stride ) <EOL> stride = int ( stride ) <EOL> num_filters , h_rows , h_cols , batch_size = hidacts . shape <EOL> channels , filter_rows , filter_cols , _num_filters = filters . shape <EOL> assert filter_cols == filter_cols <EOL> assert num_filters == _num_filters <EOL> assert stride <= filter_rows and stride >= <NUM_LIT:1> <EOL> if stride > <NUM_LIT:1> : <EOL> assert img_shape != None <EOL> rows , cols = img_shape <EOL> if ( rows - filter_rows ) % stride == <NUM_LIT:0> : <EOL> stride_padding_rows = <NUM_LIT:0> <EOL> else : <EOL> stride_padding_rows = ( ( rows - filter_rows ) / stride + <NUM_LIT:1> ) * stride + filter_rows - rows <EOL> idx_rows = ( rows + stride_padding_rows - filter_rows ) / stride <EOL> if ( cols - filter_cols ) % stride == <NUM_LIT:0> : <EOL> stride_padding_cols = <NUM_LIT:0> <EOL> else : <EOL> stride_padding_cols = ( ( cols - filter_cols ) / stride + <NUM_LIT:1> ) * stride + filter_cols - cols <EOL> idx_cols = ( cols + stride_padding_cols - filter_cols ) / stride <EOL> new_rows = rows + stride_padding_rows <EOL> new_cols = cols + stride_padding_cols <EOL> idx_rows = ( new_rows - filter_rows ) / stride <EOL> idx_cols = ( new_cols - filter_cols ) / stride <EOL> images = np . zeros ( ( channels , new_rows , new_cols , batch_size ) , dtype = '<STR_LIT>' ) <EOL> else : <EOL> rows = h_rows + filter_rows - <NUM_LIT:1> <EOL> cols = h_cols + filter_cols - <NUM_LIT:1> <EOL> img_shape = ( channels , <EOL> rows , <EOL> cols , <EOL> batch_size ) <EOL> images = np . zeros ( img_shape , dtype = '<STR_LIT>' ) <EOL> n_dim_filter = channels * filter_rows * filter_cols <EOL> vector_filters = filters . reshape ( n_dim_filter , num_filters ) . T <EOL> for idx_h_rows in xrange ( h_rows ) : <EOL> for idx_h_cols in xrange ( h_cols ) : <EOL> rc_hidacts = hidacts [ : , idx_h_rows , idx_h_cols , : ] <EOL> rc_image = ( np . dot ( <EOL> rc_hidacts . T , <EOL> vector_filters ) . T ) . reshape ( channels , filter_rows , filter_cols , batch_size ) <EOL> images [ : , <EOL> idx_h_rows * stride : idx_h_rows * stride + filter_rows , <EOL> idx_h_cols * stride : idx_h_cols * stride + filter_cols , <EOL> : ] += rc_image <EOL> rval = images [ : , : rows , : cols , : ] <EOL> return rval <EOL> def test_image_acts_strided ( ) : <EOL> rng = np . random . RandomState ( [ <NUM_LIT> , <NUM_LIT:10> , <NUM_LIT:9> ] ) <EOL> shape_list = [ [ ( <NUM_LIT:1> , <NUM_LIT:7> , <NUM_LIT:8> , <NUM_LIT:5> ) , ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:16> ) ] , <EOL> [ ( <NUM_LIT:3> , <NUM_LIT:7> , <NUM_LIT:8> , <NUM_LIT:5> ) , ( <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT:16> ) ] , <EOL> [ ( <NUM_LIT:16> , <NUM_LIT:11> , <NUM_LIT:11> , <NUM_LIT:4> ) , ( <NUM_LIT:16> , <NUM_LIT:4> , <NUM_LIT:4> , <NUM_LIT:16> ) ] , <EOL> [ ( <NUM_LIT:3> , <NUM_LIT:20> , <NUM_LIT:20> , <NUM_LIT:3> ) , ( <NUM_LIT:3> , <NUM_LIT:5> , <NUM_LIT:5> , <NUM_LIT:16> ) ] , <EOL> [ ( <NUM_LIT:3> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:3> ) , ( <NUM_LIT:3> , <NUM_LIT:6> , <NUM_LIT:6> , <NUM_LIT:16> ) ] , <EOL> ] <EOL> for test_idx in xrange ( len ( shape_list ) ) : <EOL> images = rng . uniform ( - <NUM_LIT:1.> , <NUM_LIT:1.> , shape_list [ test_idx ] [ <NUM_LIT:0> ] ) . astype ( '<STR_LIT>' ) <EOL> filters = rng . uniform ( - <NUM_LIT:1.> , <NUM_LIT:1.> , shape_list [ test_idx ] [ <NUM_LIT:1> ] ) . astype ( '<STR_LIT>' ) <EOL> gpu_images = float32_shared_constructor ( images , name = '<STR_LIT>' ) <EOL> gpu_filters = float32_shared_constructor ( filters , name = '<STR_LIT>' ) <EOL> print ( "<STR_LIT>" % ( test_idx + <NUM_LIT:1> ) ) <EOL> for ii in xrange ( filters . shape [ <NUM_LIT:1> ] ) : <EOL> stride = ii + <NUM_LIT:1> <EOL> output_python = FilterActs_python ( images , filters , stride ) <EOL> hidacts = rng . uniform ( - <NUM_LIT:1.> , <NUM_LIT:1.> , output_python . shape ) . astype ( '<STR_LIT>' ) <EOL> gpu_hidacts = float32_shared_constructor ( hidacts , name = '<STR_LIT>' ) <EOL> Img_output_python = ImageActs_python ( filters , hidacts , stride , ( images . shape [ <NUM_LIT:1> ] , images . shape [ <NUM_LIT:2> ] ) ) <EOL> Img_output = ImageActs ( stride = stride ) ( gpu_hidacts , gpu_filters , as_tensor_variable ( ( images . shape [ <NUM_LIT:1> ] , images . shape [ <NUM_LIT:2> ] ) ) ) <EOL> Img_output = host_from_gpu ( Img_output ) <EOL> f = function ( [ ] , Img_output ) <EOL> Img_output_val = f ( ) <EOL> warnings . warn ( """<STR_LIT>""" ) <EOL> if np . abs ( Img_output_val - Img_output_python ) . max ( ) > <NUM_LIT> : <EOL> assert type ( Img_output_val ) == type ( Img_output_python ) <EOL> assert Img_output_val . dtype == Img_output_python . dtype <EOL> if Img_output_val . shape != Img_output_python . shape : <EOL> print ( '<STR_LIT>' , Img_output_val . shape ) <EOL> print ( '<STR_LIT>' , Img_output_python . shape ) <EOL> assert False <EOL> err = np . abs ( Img_output_val - Img_output_python ) <EOL> print ( '<STR_LIT>' % stride ) <EOL> print ( '<STR_LIT>' , ( err . min ( ) , err . max ( ) ) ) <EOL> print ( '<STR_LIT>' , err . mean ( ) ) <EOL> print ( '<STR_LIT>' , ( Img_output_val . min ( ) , Img_output_val . max ( ) ) ) <EOL> print ( '<STR_LIT>' , ( Img_output_python . min ( ) , Img_output_python . max ( ) ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> test_image_acts_strided ( ) </s>
<s> """<STR_LIT>""" <EOL> import numpy as np <EOL> import theano <EOL> from pylearn2 . sandbox . nlp . linear . matrixmul import MatrixMul <EOL> from pylearn2 . utils import sharedX <EOL> from theano import tensor <EOL> def test_matrixmul ( ) : <EOL> """<STR_LIT>""" <EOL> rng = np . random . RandomState ( <NUM_LIT> ) <EOL> dtypes = [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' <EOL> ] <EOL> tensor_x = [ <EOL> tensor . wmatrix ( ) , <EOL> tensor . imatrix ( ) , <EOL> tensor . lmatrix ( ) , <EOL> tensor . wvector ( ) , <EOL> tensor . ivector ( ) , <EOL> tensor . lvector ( ) <EOL> ] <EOL> np_W , np_x = [ ] , [ ] <EOL> for dtype in dtypes : <EOL> np_W . append ( rng . rand ( <NUM_LIT:10> , np . random . randint ( <NUM_LIT:1> , <NUM_LIT:10> ) ) ) <EOL> np_x . append ( rng . randint ( <EOL> <NUM_LIT:0> , <NUM_LIT:10> , ( rng . random_integers ( <NUM_LIT:5> ) , <EOL> rng . random_integers ( <NUM_LIT:5> ) ) <EOL> ) . astype ( dtype ) ) <EOL> for dtype in dtypes : <EOL> np_W . append ( rng . rand ( <NUM_LIT:10> , np . random . randint ( <NUM_LIT:1> , <NUM_LIT:10> ) ) ) <EOL> np_x . append ( <EOL> rng . randint ( <NUM_LIT:0> , <NUM_LIT:10> , ( rng . random_integers ( <NUM_LIT:5> ) , ) ) . astype ( dtype ) <EOL> ) <EOL> tensor_W = [ sharedX ( W ) for W in np_W ] <EOL> matrixmul = [ MatrixMul ( W ) for W in tensor_W ] <EOL> assert all ( mm . get_params ( ) [ <NUM_LIT:0> ] == W for mm , W in zip ( matrixmul , tensor_W ) ) <EOL> fn = [ theano . function ( [ x ] , mm . project ( x ) ) <EOL> for x , mm in zip ( tensor_x , matrixmul ) ] <EOL> for W , x , f in zip ( np_W , np_x , fn ) : <EOL> W_x = W [ x ] <EOL> if x . ndim == <NUM_LIT:2> : <EOL> W_x = W_x . reshape ( ( W_x . shape [ <NUM_LIT:0> ] , np . prod ( W_x . shape [ <NUM_LIT:1> : ] ) ) ) <EOL> else : <EOL> W_x = W_x . flatten ( ) <EOL> np . testing . assert_allclose ( f ( x ) , W_x ) </s>
<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> from pylearn2 . utils import serial <EOL> from pylearn2 . datasets import preprocessing <EOL> from pylearn2 . utils import string_utils <EOL> import numpy as np <EOL> from pylearn2 . datasets . cifar10 import CIFAR10 <EOL> import textwrap <EOL> def main ( ) : <EOL> data_dir = string_utils . preprocess ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' ) <EOL> train = CIFAR10 ( which_set = '<STR_LIT:train>' ) <EOL> print ( "<STR_LIT>" ) <EOL> output_dir = data_dir + '<STR_LIT>' <EOL> serial . mkdir ( output_dir ) <EOL> README = open ( output_dir + '<STR_LIT>' , '<STR_LIT:w>' ) <EOL> README . write ( textwrap . dedent ( """<STR_LIT>""" ) ) <EOL> README . close ( ) <EOL> print ( "<STR_LIT>" ) <EOL> preprocessor = preprocessing . ZCA ( ) <EOL> train . apply_preprocessor ( preprocessor = preprocessor , can_fit = True ) <EOL> print ( '<STR_LIT>' ) <EOL> train . use_design_loc ( output_dir + '<STR_LIT>' ) <EOL> serial . save ( output_dir + '<STR_LIT>' , train ) <EOL> print ( "<STR_LIT>" ) <EOL> test = CIFAR10 ( which_set = '<STR_LIT:test>' ) <EOL> print ( "<STR_LIT>" ) <EOL> test . apply_preprocessor ( preprocessor = preprocessor , can_fit = False ) <EOL> print ( "<STR_LIT>" ) <EOL> test . use_design_loc ( output_dir + '<STR_LIT>' ) <EOL> serial . save ( output_dir + '<STR_LIT>' , test ) <EOL> serial . save ( output_dir + '<STR_LIT>' , preprocessor ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) </s>
<s> from __future__ import print_function <EOL> import numpy as np <EOL> import os <EOL> import sys <EOL> from pylearn2 . utils import serial <EOL> from pylearn2 . utils . string_utils import preprocess <EOL> def usage ( ) : <EOL> print ( """<STR_LIT>""" ) <EOL> if len ( sys . argv ) != <NUM_LIT:2> : <EOL> usage ( ) <EOL> print ( '<STR_LIT>' ) <EOL> quit ( - <NUM_LIT:1> ) <EOL> _ , arg = sys . argv <EOL> if arg == '<STR_LIT>' : <EOL> base = preprocess ( '<STR_LIT>' ) <EOL> expected_num_images = <NUM_LIT> <EOL> elif arg == '<STR_LIT>' : <EOL> base = preprocess ( '<STR_LIT>' ) <EOL> expected_num_images = <NUM_LIT> <EOL> else : <EOL> usage ( ) <EOL> print ( '<STR_LIT>' , arg ) <EOL> print ( '<STR_LIT>' ) <EOL> outdir = base [ : - <NUM_LIT:3> ] + '<STR_LIT>' <EOL> serial . mkdir ( outdir ) <EOL> paths = os . listdir ( base ) <EOL> if len ( paths ) != expected_num_images : <EOL> raise AssertionError ( "<STR_LIT>" + base + "<STR_LIT>" + str ( expected_num_images ) + "<STR_LIT>" + str ( len ( paths ) ) ) <EOL> means = np . load ( '<STR_LIT>' ) <EOL> norms = <NUM_LIT> + np . sqrt ( np . square ( means ) . sum ( axis = <NUM_LIT:3> ) . sum ( axis = <NUM_LIT:2> ) . sum ( axis = <NUM_LIT:1> ) ) <EOL> means = np . transpose ( np . transpose ( means , ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:0> ) ) / norms , ( <NUM_LIT:3> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ) ) <EOL> from pylearn2 . utils import sharedX <EOL> kernels = sharedX ( np . transpose ( means , ( <NUM_LIT:0> , <NUM_LIT:3> , <NUM_LIT:1> , <NUM_LIT:2> ) ) ) <EOL> import theano . tensor as T <EOL> X = T . TensorType ( broadcastable = ( False , False , False ) , dtype = kernels . dtype ) ( ) <EOL> bc01 = X . dimshuffle ( '<STR_LIT:x>' , <NUM_LIT:2> , <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> Z = T . nnet . conv2d ( input = bc01 , filters = kernels , subsample = ( means . shape [ <NUM_LIT:1> ] / <NUM_LIT:2> , means . shape [ <NUM_LIT:2> ] / <NUM_LIT:2> ) , <EOL> filter_shape = kernels . get_value ( ) . shape ) <EOL> F = T . signal . downsample . max_pool_2d ( Z , ( <NUM_LIT:2> , <NUM_LIT:2> ) ) <EOL> F = T . clip ( F - <NUM_LIT> , <NUM_LIT:0.> , <NUM_LIT> ) <EOL> from theano import function <EOL> f = function ( [ X ] , F ) <EOL> for i , path in enumerate ( paths ) : <EOL> print ( i ) <EOL> try : <EOL> X = np . load ( base + '<STR_LIT:/>' + path ) <EOL> assert X . ndim == <NUM_LIT:3> <EOL> F = f ( X ) <EOL> np . save ( outdir + '<STR_LIT:/>' + path , F ) <EOL> except Exception as e : <EOL> raise </s>
<s> """<STR_LIT>""" <EOL> import argparse <EOL> from pylearn2 . gui import get_weights_report <EOL> def show_weights ( model_path , rescale = "<STR_LIT>" , <EOL> border = False , out = None ) : <EOL> """<STR_LIT>""" <EOL> pv = get_weights_report . get_weights_report ( model_path = model_path , <EOL> rescale = rescale , <EOL> border = border ) <EOL> if out is None : <EOL> pv . show ( ) <EOL> else : <EOL> pv . save ( out ) <EOL> def make_argument_parser ( ) : <EOL> """<STR_LIT>""" <EOL> parser = argparse . ArgumentParser ( ) <EOL> parser . add_argument ( "<STR_LIT>" , default = "<STR_LIT>" ) <EOL> parser . add_argument ( "<STR_LIT>" , default = None ) <EOL> parser . add_argument ( "<STR_LIT>" , action = "<STR_LIT:store_true>" , default = False ) <EOL> parser . add_argument ( "<STR_LIT:path>" ) <EOL> return parser <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> parser = make_argument_parser ( ) <EOL> args = parser . parse_args ( ) <EOL> show_weights ( args . path , args . rescale , args . border , args . out ) </s>
<s> """<STR_LIT>""" <EOL> from pylearn2 . termination_criteria import EpochCounter <EOL> from pylearn2 . datasets . dense_design_matrix import DenseDesignMatrix <EOL> from pylearn2 . models . mlp import MLP , Softmax <EOL> from pylearn2 . monitor import push_monitor <EOL> from pylearn2 . train import Train <EOL> from pylearn2 . training_algorithms . sgd import SGD <EOL> import numpy as np <EOL> def test_epoch_counter ( ) : <EOL> """<STR_LIT>""" <EOL> def produce_train_obj ( new_epochs , max_epochs , model = None ) : <EOL> if model is None : <EOL> model = MLP ( layers = [ Softmax ( layer_name = '<STR_LIT:y>' , <EOL> n_classes = <NUM_LIT:2> , <EOL> irange = <NUM_LIT:0.> ) ] , <EOL> nvis = <NUM_LIT:3> ) <EOL> else : <EOL> model = push_monitor ( model , '<STR_LIT>' , <EOL> transfer_experience = True ) <EOL> dataset = DenseDesignMatrix ( X = np . random . normal ( size = ( <NUM_LIT:6> , <NUM_LIT:3> ) ) , <EOL> y = np . random . normal ( size = ( <NUM_LIT:6> , <NUM_LIT:2> ) ) ) <EOL> epoch_counter = EpochCounter ( max_epochs = max_epochs , <EOL> new_epochs = new_epochs ) <EOL> algorithm = SGD ( batch_size = <NUM_LIT:2> , <EOL> learning_rate = <NUM_LIT:0.1> , <EOL> termination_criterion = epoch_counter ) <EOL> return Train ( dataset = dataset , model = model , algorithm = algorithm ) <EOL> def test_epochs ( epochs_seen , n ) : <EOL> assert epochs_seen == n , "<STR_LIT>" % ( epochs_seen , n ) <EOL> train_obj = produce_train_obj ( new_epochs = True , max_epochs = <NUM_LIT:5> ) <EOL> train_obj . main_loop ( ) <EOL> test_epochs ( train_obj . model . monitor . get_epochs_seen ( ) , <NUM_LIT:5> ) <EOL> train_obj = produce_train_obj ( new_epochs = True , <EOL> max_epochs = <NUM_LIT:5> , <EOL> model = train_obj . model ) <EOL> train_obj . main_loop ( ) <EOL> test_epochs ( train_obj . model . monitor . get_epochs_seen ( ) , <NUM_LIT:2> * <NUM_LIT:5> ) <EOL> train_obj = produce_train_obj ( new_epochs = False , max_epochs = <NUM_LIT:5> ) <EOL> train_obj . main_loop ( ) <EOL> test_epochs ( train_obj . model . monitor . get_epochs_seen ( ) , <NUM_LIT:5> ) <EOL> train_obj = produce_train_obj ( new_epochs = False , <EOL> max_epochs = <NUM_LIT:5> , <EOL> model = train_obj . model ) <EOL> train_obj . main_loop ( ) <EOL> test_epochs ( train_obj . model . monitor . get_epochs_seen ( ) , <NUM_LIT:5> ) <EOL> train_obj = produce_train_obj ( new_epochs = True , max_epochs = <NUM_LIT:0> ) <EOL> before_train = train_obj . model . get_weights ( ) <EOL> train_obj . main_loop ( ) <EOL> after_train = train_obj . model . get_weights ( ) <EOL> test_epochs ( train_obj . model . monitor . get_epochs_seen ( ) , <NUM_LIT:0> ) <EOL> assert np . all ( before_train == after_train ) <EOL> train_obj = produce_train_obj ( new_epochs = True , <EOL> max_epochs = <NUM_LIT:0> , <EOL> model = train_obj . model ) <EOL> before_train = train_obj . model . get_weights ( ) <EOL> train_obj . main_loop ( ) <EOL> after_train = train_obj . model . get_weights ( ) <EOL> test_epochs ( train_obj . model . monitor . get_epochs_seen ( ) , <NUM_LIT:0> ) <EOL> assert np . all ( before_train == after_train ) <EOL> train_obj = produce_train_obj ( new_epochs = False , <EOL> max_epochs = <NUM_LIT:0> , <EOL> model = train_obj . model ) <EOL> before_train = train_obj . model . get_weights ( ) <EOL> train_obj . main_loop ( ) <EOL> after_train = train_obj . model . get_weights ( ) <EOL> test_epochs ( train_obj . model . monitor . get_epochs_seen ( ) , <NUM_LIT:0> ) <EOL> assert np . all ( before_train == after_train ) </s>
<s> """<STR_LIT>""" <EOL> __author__ = "<STR_LIT>" <EOL> __copyright__ = "<STR_LIT>" <EOL> __license__ = "<STR_LIT>" <EOL> __maintainer__ = "<STR_LIT>" <EOL> from theano import config <EOL> from theano import tensor as T <EOL> from pylearn2 . train_extensions import TrainExtension <EOL> class WMapeNumeratorChannel ( TrainExtension ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , channel_name_suffix = '<STR_LIT>' ) : <EOL> self . channel_name_suffix = channel_name_suffix <EOL> def setup ( self , model , dataset , algorithm ) : <EOL> """<STR_LIT>""" <EOL> m_space , m_source = model . get_monitoring_data_specs ( ) <EOL> state , target = m_space . make_theano_batch ( ) <EOL> y = target [ : , <NUM_LIT:0> ] <EOL> y_hat = model . fprop ( state ) [ : , <NUM_LIT:0> ] <EOL> wmape_numerator = abs ( y - y_hat ) . sum ( ) <EOL> wmape_numerator = T . cast ( wmape_numerator , config . floatX ) <EOL> for dataset_name , dataset in algorithm . monitoring_dataset . items ( ) : <EOL> if dataset_name : <EOL> channel_name = '<STR_LIT>' . format ( dataset_name , <EOL> self . channel_name_suffix ) <EOL> else : <EOL> channel_name = self . channel_name_suffix <EOL> model . monitor . add_channel ( name = channel_name , <EOL> ipt = ( state , target ) , <EOL> val = wmape_numerator , <EOL> data_specs = ( m_space , m_source ) , <EOL> dataset = dataset ) <EOL> class WMapeDenominatorChannel ( TrainExtension ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , channel_name_suffix = '<STR_LIT>' ) : <EOL> self . channel_name_suffix = channel_name_suffix <EOL> def setup ( self , model , dataset , algorithm ) : <EOL> """<STR_LIT>""" <EOL> m_space , m_source = model . get_monitoring_data_specs ( ) <EOL> state , target = m_space . make_theano_batch ( ) <EOL> y = target [ : , <NUM_LIT:0> ] <EOL> wmape_denominator = abs ( y ) . sum ( ) <EOL> wmape_denominator = T . cast ( wmape_denominator , config . floatX ) <EOL> for dataset_name , dataset in algorithm . monitoring_dataset . items ( ) : <EOL> if dataset_name : <EOL> channel_name = '<STR_LIT>' . format ( dataset_name , <EOL> self . channel_name_suffix ) <EOL> else : <EOL> channel_name = self . channel_name_suffix <EOL> model . monitor . add_channel ( name = channel_name , <EOL> ipt = ( state , target ) , <EOL> val = wmape_denominator , <EOL> data_specs = ( m_space , m_source ) , <EOL> dataset = dataset ) </s>
<s> """<STR_LIT>""" <EOL> import functools <EOL> import sys <EOL> if sys . version_info [ : <NUM_LIT:2> ] < ( <NUM_LIT:2> , <NUM_LIT:7> ) : <EOL> def cmp_to_key ( mycmp ) : <EOL> """<STR_LIT>""" <EOL> class K ( object ) : <EOL> __slots__ = [ '<STR_LIT>' ] <EOL> def __init__ ( self , obj , * args ) : <EOL> self . obj = obj <EOL> def __lt__ ( self , other ) : <EOL> return mycmp ( self . obj , other . obj ) < <NUM_LIT:0> <EOL> def __gt__ ( self , other ) : <EOL> return mycmp ( self . obj , other . obj ) > <NUM_LIT:0> <EOL> def __eq__ ( self , other ) : <EOL> return mycmp ( self . obj , other . obj ) == <NUM_LIT:0> <EOL> def __le__ ( self , other ) : <EOL> return mycmp ( self . obj , other . obj ) <= <NUM_LIT:0> <EOL> def __ge__ ( self , other ) : <EOL> return mycmp ( self . obj , other . obj ) >= <NUM_LIT:0> <EOL> def __ne__ ( self , other ) : <EOL> return mycmp ( self . obj , other . obj ) != <NUM_LIT:0> <EOL> def __hash__ ( self ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> return K <EOL> else : <EOL> from functools import cmp_to_key </s>
<s> from __future__ import print_function <EOL> import warnings <EOL> from setuptools import setup , find_packages , Extension <EOL> from setuptools . command . install import install <EOL> import numpy <EOL> from theano . compat . six . moves import input <EOL> try : <EOL> from Cython . Distutils import build_ext <EOL> cython_available = True <EOL> except ImportError : <EOL> warnings . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> cython_available = False <EOL> if cython_available : <EOL> cmdclass = { '<STR_LIT>' : build_ext } <EOL> ext_modules = [ Extension ( "<STR_LIT>" , <EOL> [ "<STR_LIT>" ] , <EOL> include_dirs = [ numpy . get_include ( ) ] ) , <EOL> Extension ( "<STR_LIT>" , <EOL> [ "<STR_LIT>" ] , <EOL> include_dirs = [ numpy . get_include ( ) ] ) ] <EOL> else : <EOL> cmdclass = { } <EOL> ext_modules = [ ] <EOL> class pylearn2_install ( install ) : <EOL> def run ( self ) : <EOL> print ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> mode = None <EOL> while mode not in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if mode is not None : <EOL> print ( "<STR_LIT>" ) <EOL> mode = input ( "<STR_LIT>" ) <EOL> if mode in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> self . distribution . run_command ( '<STR_LIT>' ) <EOL> if mode == '<STR_LIT>' : <EOL> return install . run ( self ) <EOL> cmdclass . update ( { '<STR_LIT>' : pylearn2_install } ) <EOL> setup ( <EOL> cmdclass = cmdclass , <EOL> ext_modules = ext_modules , <EOL> name = '<STR_LIT>' , <EOL> version = '<STR_LIT>' , <EOL> packages = find_packages ( ) , <EOL> description = '<STR_LIT>' , <EOL> license = '<STR_LIT>' , <EOL> long_description = open ( '<STR_LIT>' , '<STR_LIT:rb>' ) . read ( ) . decode ( '<STR_LIT:utf8>' ) , <EOL> dependency_links = [ '<STR_LIT>' ] , <EOL> install_requires = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , "<STR_LIT>" ] , <EOL> scripts = [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] , <EOL> package_data = { <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> } , <EOL> ) </s>
<s> '''<STR_LIT>''' <EOL> from base import RauthTestCase <EOL> from test_service import HttpMixin , RequestMixin , ServiceMixin <EOL> from rauth . compat import parse_qsl , quote , is_basestring , iteritems <EOL> from rauth . service import OAuth1Service <EOL> from rauth . session import OAUTH1_DEFAULT_TIMEOUT , OAuth1Session <EOL> from rauth . utils import CaseInsensitiveDict , ENTITY_METHODS , FORM_URLENCODED <EOL> from copy import deepcopy <EOL> from hashlib import sha1 <EOL> from mock import patch <EOL> import rauth <EOL> import requests <EOL> import json <EOL> import pickle <EOL> class OAuth1ServiceTestCase ( RauthTestCase , RequestMixin , ServiceMixin , <EOL> HttpMixin ) : <EOL> consumer_key = '<STR_LIT>' <EOL> consumer_secret = '<STR_LIT>' <EOL> access_token = '<STR_LIT>' <EOL> access_token_secret = '<STR_LIT>' <EOL> def setUp ( self ) : <EOL> RauthTestCase . setUp ( self ) <EOL> self . request_token_url = '<STR_LIT>' <EOL> self . access_token_url = '<STR_LIT>' <EOL> self . authorize_url = '<STR_LIT>' <EOL> self . base_url = '<STR_LIT>' <EOL> self . service = OAuth1Service ( self . consumer_key , <EOL> self . consumer_secret , <EOL> name = '<STR_LIT>' , <EOL> request_token_url = self . request_token_url , <EOL> access_token_url = self . access_token_url , <EOL> authorize_url = self . authorize_url , <EOL> base_url = self . base_url ) <EOL> self . session = self . service . get_session ( ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> self . session . request = self . fake_request <EOL> self . service . get_session = self . fake_get_session <EOL> def fake_get_auth_header ( self , oauth_params , realm = None ) : <EOL> auth_header = '<STR_LIT>' . format ( realm = realm ) <EOL> params = '<STR_LIT>' <EOL> for k , v in iteritems ( oauth_params ) : <EOL> params += '<STR_LIT>' . format ( key = k , value = quote ( str ( v ) ) ) <EOL> auth_header += params <EOL> return auth_header <EOL> @ patch . object ( rauth . session . HmacSha1Signature , '<STR_LIT>' ) <EOL> @ patch . object ( rauth . session , '<STR_LIT:time>' ) <EOL> @ patch . object ( rauth . session , '<STR_LIT>' ) <EOL> @ patch . object ( requests . Session , '<STR_LIT>' ) <EOL> def fake_request ( self , <EOL> method , <EOL> url , <EOL> mock_request , <EOL> mock_random , <EOL> mock_time , <EOL> mock_sig , <EOL> header_auth = False , <EOL> realm = '<STR_LIT>' , <EOL> ** kwargs ) : <EOL> fake_random = <NUM_LIT:1> <EOL> fake_time = <NUM_LIT:1> <EOL> fake_sig = '<STR_LIT:foo>' <EOL> fake_sig_meth = '<STR_LIT>' <EOL> fake_nonce = sha1 ( str ( fake_random ) . encode ( '<STR_LIT:ascii>' ) ) . hexdigest ( ) <EOL> mock_request . return_value = self . response <EOL> mock_random . return_value = fake_random <EOL> mock_time . return_value = fake_time <EOL> mock_sig . return_value = fake_sig <EOL> method = method <EOL> url = self . session . _set_url ( url ) <EOL> service = OAuth1Service ( self . consumer_key , <EOL> self . consumer_secret , <EOL> name = '<STR_LIT>' , <EOL> request_token_url = self . request_token_url , <EOL> access_token_url = self . access_token_url , <EOL> authorize_url = self . authorize_url , <EOL> base_url = self . base_url ) <EOL> session = service . get_session ( ( self . access_token , <EOL> self . access_token_secret ) ) <EOL> r = session . request ( method , <EOL> url , <EOL> header_auth = header_auth , <EOL> realm = realm , <EOL> ** deepcopy ( kwargs ) ) <EOL> kwargs . setdefault ( '<STR_LIT>' , { } ) <EOL> kwargs [ '<STR_LIT>' ] = CaseInsensitiveDict ( kwargs [ '<STR_LIT>' ] ) <EOL> entity_method = method . upper ( ) in ENTITY_METHODS <EOL> if entity_method : <EOL> kwargs [ '<STR_LIT>' ] . setdefault ( '<STR_LIT:Content-Type>' , FORM_URLENCODED ) <EOL> form_urlencoded = kwargs [ '<STR_LIT>' ] . get ( '<STR_LIT:Content-Type>' ) == FORM_URLENCODED <EOL> if is_basestring ( kwargs . get ( '<STR_LIT>' ) ) : <EOL> kwargs [ '<STR_LIT>' ] = dict ( parse_qsl ( kwargs [ '<STR_LIT>' ] ) ) <EOL> if is_basestring ( kwargs . get ( '<STR_LIT:data>' ) ) and form_urlencoded : <EOL> kwargs [ '<STR_LIT:data>' ] = dict ( parse_qsl ( kwargs [ '<STR_LIT:data>' ] ) ) <EOL> oauth_params = { '<STR_LIT>' : session . consumer_key , <EOL> '<STR_LIT>' : fake_nonce , <EOL> '<STR_LIT>' : fake_sig_meth , <EOL> '<STR_LIT>' : fake_time , <EOL> '<STR_LIT>' : self . access_token , <EOL> '<STR_LIT>' : session . VERSION , <EOL> '<STR_LIT>' : fake_sig } <EOL> if header_auth : <EOL> auth = mock_request . call_args [ <NUM_LIT:1> ] [ '<STR_LIT>' ] <EOL> auth_header = self . fake_get_auth_header ( oauth_params , realm = realm ) <EOL> self . assertEqual ( auth ( requests . Request ( ) ) . headers [ '<STR_LIT>' ] , <EOL> auth_header ) <EOL> kwargs [ '<STR_LIT>' ] = auth <EOL> elif entity_method : <EOL> kwargs [ '<STR_LIT:data>' ] = kwargs . get ( '<STR_LIT:data>' ) or { } <EOL> if form_urlencoded : <EOL> kwargs [ '<STR_LIT:data>' ] . update ( oauth_params ) <EOL> else : <EOL> kwargs . setdefault ( '<STR_LIT>' , { } ) <EOL> kwargs [ '<STR_LIT>' ] . update ( oauth_params ) <EOL> else : <EOL> kwargs . setdefault ( '<STR_LIT>' , { } ) <EOL> kwargs [ '<STR_LIT>' ] . update ( ** oauth_params ) <EOL> mock_request . assert_called_with ( method , <EOL> url , <EOL> timeout = OAUTH1_DEFAULT_TIMEOUT , <EOL> ** kwargs ) <EOL> return r <EOL> def fake_get_session ( self , token = None , signature = None ) : <EOL> return self . session <EOL> def test_get_session ( self ) : <EOL> s = self . service . get_session ( ) <EOL> self . assertIsInstance ( s , OAuth1Session ) <EOL> def test_get_raw_request_token ( self ) : <EOL> resp = '<STR_LIT>' <EOL> self . response . content = resp <EOL> r = self . service . get_raw_request_token ( ) <EOL> self . assertEqual ( r . content , resp ) <EOL> def test_get_raw_request_token_missing_request_token_url ( self ) : <EOL> self . service . request_token_url = None <EOL> resp = '<STR_LIT>' <EOL> self . response . content = resp <EOL> with self . assertRaises ( TypeError ) as e : <EOL> self . service . get_raw_request_token ( ) <EOL> self . assertEqual ( str ( e . exception ) , <EOL> '<STR_LIT>' ) <EOL> def test_get_request_token ( self ) : <EOL> self . response . content = '<STR_LIT>' <EOL> request_token , request_token_secret = self . service . get_request_token ( ) <EOL> self . assertEqual ( request_token , '<STR_LIT:foo>' ) <EOL> self . assertEqual ( request_token_secret , '<STR_LIT:bar>' ) <EOL> def test_get_request_token_with_json_decoder ( self ) : <EOL> self . response . content = json . dumps ( { '<STR_LIT>' : '<STR_LIT:foo>' , <EOL> '<STR_LIT>' : '<STR_LIT:bar>' } ) <EOL> request_token , request_token_secret = self . service . get_request_token ( decoder = json . loads ) <EOL> self . assertEqual ( request_token , '<STR_LIT:foo>' ) <EOL> self . assertEqual ( request_token_secret , '<STR_LIT:bar>' ) <EOL> def test_get_authorize_url ( self ) : <EOL> self . response . content = '<STR_LIT>' <EOL> request_token , request_token_secret = self . service . get_request_token ( ) <EOL> url = self . service . get_authorize_url ( request_token ) <EOL> expected_fmt = '<STR_LIT>' <EOL> self . assertEqual ( url , expected_fmt . format ( request_token ) ) <EOL> def test_get_authorize_url_with_url_encoded_characters ( self ) : <EOL> token = '<STR_LIT>' <EOL> token_secret = '<STR_LIT>' <EOL> response_fmt = '<STR_LIT>' <EOL> self . response . content = response_fmt . format ( token , token_secret ) <EOL> request_token , request_token_secret = self . service . get_request_token ( ) <EOL> url = self . service . get_authorize_url ( request_token ) <EOL> expected_fmt = '<STR_LIT>' <EOL> self . assertEqual ( url , expected_fmt . format ( token ) ) <EOL> def test_get_raw_access_token ( self ) : <EOL> self . response . content = '<STR_LIT>' <EOL> request_token , request_token_secret = self . service . get_request_token ( ) <EOL> resp = '<STR_LIT>' <EOL> self . response . content = resp <EOL> r = self . service . get_raw_access_token ( request_token , <EOL> request_token_secret ) <EOL> self . assertEqual ( r . content , resp ) <EOL> def test_get_raw_access_token_missing_access_token_url ( self ) : <EOL> self . response . content = '<STR_LIT>' <EOL> request_token , request_token_secret = self . service . get_request_token ( ) <EOL> self . service . access_token_url = None <EOL> self . response . content = '<STR_LIT>' <EOL> with self . assertRaises ( TypeError ) as e : <EOL> self . service . get_raw_access_token ( request_token , <EOL> request_token_secret ) <EOL> self . assertEqual ( str ( e . exception ) , <EOL> '<STR_LIT>' ) <EOL> def test_get_access_token ( self ) : <EOL> self . response . content = '<STR_LIT>' <EOL> request_token , request_token_secret = self . service . get_request_token ( ) <EOL> self . response . content = '<STR_LIT>' <EOL> access_token , access_token_secret = self . service . get_access_token ( request_token , <EOL> request_token_secret ) <EOL> self . assertEqual ( access_token , '<STR_LIT:foo>' ) <EOL> self . assertEqual ( access_token_secret , '<STR_LIT:bar>' ) <EOL> def test_get_access_token_with_json_decoder ( self ) : <EOL> self . response . content = '<STR_LIT>' <EOL> request_token , request_token_secret = self . service . get_request_token ( ) <EOL> self . response . content = json . dumps ( { '<STR_LIT>' : '<STR_LIT:foo>' , <EOL> '<STR_LIT>' : '<STR_LIT:bar>' } ) <EOL> access_token , access_token_secret = self . service . get_access_token ( request_token , <EOL> request_token_secret , <EOL> decoder = json . loads ) <EOL> self . assertEqual ( access_token , '<STR_LIT:foo>' ) <EOL> self . assertEqual ( access_token_secret , '<STR_LIT:bar>' ) <EOL> def test_request_with_optional_params_oauth_callback ( self ) : <EOL> params = { '<STR_LIT>' : '<STR_LIT>' } <EOL> r = self . session . request ( '<STR_LIT:GET>' , '<STR_LIT>' , params = params ) <EOL> self . assert_ok ( r ) <EOL> def test_request_with_optional_params_oauth_verifier ( self ) : <EOL> params = { '<STR_LIT>' : '<STR_LIT:foo>' } <EOL> r = self . session . request ( '<STR_LIT:GET>' , '<STR_LIT>' , params = params ) <EOL> self . assert_ok ( r ) <EOL> def test_request_with_optional_params_oauth_version ( self ) : <EOL> params = { '<STR_LIT>' : '<STR_LIT:foo>' } <EOL> r = self . session . request ( '<STR_LIT:GET>' , '<STR_LIT>' , params = params ) <EOL> self . assert_ok ( r ) <EOL> def test_request_with_optional_params_as_string ( self ) : <EOL> params = '<STR_LIT>' <EOL> r = self . session . request ( '<STR_LIT:GET>' , '<STR_LIT>' , params = params ) <EOL> self . assert_ok ( r ) <EOL> def test_request_with_optional_data_as_string ( self ) : <EOL> data = '<STR_LIT>' <EOL> r = self . session . request ( '<STR_LIT:POST>' , '<STR_LIT>' , data = data ) <EOL> self . assert_ok ( r ) <EOL> def test_request_with_optional_params_with_data ( self ) : <EOL> data = { '<STR_LIT>' : '<STR_LIT>' } <EOL> r = self . session . request ( '<STR_LIT:POST>' , '<STR_LIT>' , data = data ) <EOL> self . assert_ok ( r ) <EOL> def test_request_with_header_auth ( self ) : <EOL> r = self . session . request ( '<STR_LIT:GET>' , <EOL> '<STR_LIT>' , <EOL> header_auth = True ) <EOL> self . assert_ok ( r ) <EOL> def test_request_with_header_auth_with_realm ( self ) : <EOL> r = self . session . request ( '<STR_LIT:GET>' , <EOL> '<STR_LIT>' , <EOL> header_auth = True , <EOL> realm = '<STR_LIT>' ) <EOL> self . assert_ok ( r ) <EOL> def test_get_auth_session ( self ) : <EOL> resp = '<STR_LIT>' <EOL> self . response . content = resp <EOL> s = self . service . get_auth_session ( '<STR_LIT:foo>' , '<STR_LIT:bar>' ) <EOL> self . assertIsInstance ( s , OAuth1Session ) <EOL> def test_get_auth_session_with_request_token_response ( self ) : <EOL> resp = '<STR_LIT>' <EOL> self . response . content = resp <EOL> self . service . request_token_response = '<STR_LIT>' <EOL> s = self . service . get_auth_session ( '<STR_LIT:foo>' , '<STR_LIT:bar>' ) <EOL> self . assertEqual ( s . request_token_response , '<STR_LIT>' ) <EOL> def test_pickle_session ( self ) : <EOL> session = pickle . loads ( pickle . dumps ( self . session ) ) <EOL> session . request = self . fake_request <EOL> r = session . request ( '<STR_LIT:GET>' , '<STR_LIT>' , header_auth = True ) <EOL> self . assert_ok ( r ) </s>
<s> from django . db import models , migrations <EOL> import django . core . validators <EOL> class Migration ( migrations . Migration ) : <EOL> dependencies = [ <EOL> ] <EOL> operations = [ <EOL> migrations . CreateModel ( <EOL> name = '<STR_LIT>' , <EOL> fields = [ <EOL> ( u'<STR_LIT:id>' , models . AutoField ( verbose_name = u'<STR_LIT>' , serialize = False , auto_created = True , primary_key = True ) ) , <EOL> ( '<STR_LIT>' , models . CharField ( max_length = <NUM_LIT:200> ) ) , <EOL> ( '<STR_LIT>' , models . ImageField ( upload_to = '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , models . CharField ( max_length = <NUM_LIT:100> ) ) , <EOL> ] , <EOL> options = { <EOL> } , <EOL> bases = ( models . Model , ) , <EOL> ) , <EOL> migrations . CreateModel ( <EOL> name = '<STR_LIT>' , <EOL> fields = [ <EOL> ( u'<STR_LIT:id>' , models . AutoField ( verbose_name = u'<STR_LIT>' , serialize = False , auto_created = True , primary_key = True ) ) , <EOL> ( '<STR_LIT>' , models . CharField ( max_length = <NUM_LIT> ) ) , <EOL> ( '<STR_LIT>' , models . PositiveIntegerField ( validators = [ django . core . validators . MinValueValidator ( <NUM_LIT:0> ) ] ) ) , <EOL> ( '<STR_LIT>' , models . PositiveIntegerField ( validators = [ django . core . validators . MinValueValidator ( <NUM_LIT:0> ) ] ) ) , <EOL> ( '<STR_LIT>' , models . PositiveIntegerField ( validators = [ django . core . validators . MinValueValidator ( <NUM_LIT:1> ) ] ) ) , <EOL> ( '<STR_LIT>' , models . PositiveIntegerField ( validators = [ django . core . validators . MinValueValidator ( <NUM_LIT:1> ) ] ) ) , <EOL> ( '<STR_LIT:name>' , models . CharField ( max_length = <NUM_LIT:20> , blank = True ) ) , <EOL> ( '<STR_LIT>' , models . PositiveIntegerField ( default = <NUM_LIT:3> , validators = [ django . core . validators . MinValueValidator ( <NUM_LIT:1> ) ] ) ) , <EOL> ] , <EOL> options = { <EOL> u'<STR_LIT>' : ( '<STR_LIT>' , ) , <EOL> } , <EOL> bases = ( models . Model , ) , <EOL> ) , <EOL> ] </s>
<s> from setuptools import setup , find_packages <EOL> import platform <EOL> import os <EOL> data_files = [ ( '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] ) , <EOL> ( '<STR_LIT>' , <EOL> [ '<STR_LIT>' , '<STR_LIT>' ] ) , <EOL> ( '<STR_LIT>' , [ ] ) ] <EOL> if platform . system ( ) == '<STR_LIT>' : <EOL> data_files = [ ( '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] ) , <EOL> ( os . path . join ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> [ '<STR_LIT>' , '<STR_LIT>' ] ) ] <EOL> setup ( <EOL> name = "<STR_LIT>" , <EOL> version = "<STR_LIT>" , <EOL> description = """<STR_LIT>""" , <EOL> author = "<STR_LIT>" , <EOL> author_email = "<STR_LIT>" , <EOL> packages = find_packages ( where = '<STR_LIT:src>' ) , <EOL> package_dir = { '<STR_LIT>' : '<STR_LIT:src>' } , <EOL> data_files = data_files , <EOL> install_requires = [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] , <EOL> entry_points = { '<STR_LIT>' : [ '<STR_LIT>' ] } <EOL> ) </s>
<s> import os <EOL> import re <EOL> import time <EOL> import datetime <EOL> import hashlib <EOL> import logging <EOL> import string <EOL> import random <EOL> import base64 <EOL> from google . appengine . ext import webapp <EOL> from google . appengine . api import memcache <EOL> from google . appengine . api import urlfetch <EOL> from google . appengine . ext import db <EOL> from google . appengine . ext . webapp import util <EOL> from google . appengine . ext . webapp import template <EOL> from v2ex . babel import Member <EOL> from v2ex . babel import Counter <EOL> from v2ex . babel import Section <EOL> from v2ex . babel import Node <EOL> from v2ex . babel import Topic <EOL> from v2ex . babel import Reply <EOL> from v2ex . babel import Note <EOL> from v2ex . babel import SYSTEM_VERSION <EOL> from v2ex . babel . security import * <EOL> from v2ex . babel . ua import * <EOL> from v2ex . babel . da import * <EOL> from v2ex . babel . ext . cookies import Cookies <EOL> from django . utils import simplejson as json <EOL> template . register_template_library ( '<STR_LIT>' ) <EOL> from topic import TOPIC_PAGE_SIZE <EOL> class ApiHandler ( webapp . RequestHandler ) : <EOL> def write ( self , output ) : <EOL> if output is None : <EOL> output = '<STR_LIT>' <EOL> callback = self . request . get ( '<STR_LIT>' , None ) <EOL> if callback : <EOL> if not isinstance ( output , unicode ) : <EOL> output = output . decode ( '<STR_LIT:utf-8>' ) <EOL> self . response . headers [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> output = '<STR_LIT>' % ( callback , output ) <EOL> else : <EOL> self . response . headers [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . response . out . write ( output ) <EOL> class SiteStatsHandler ( ApiHandler ) : <EOL> def get ( self ) : <EOL> template_values = { } <EOL> template_values [ '<STR_LIT>' ] = GetKindByName ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> template_values [ '<STR_LIT>' ] = GetKindByName ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> path = os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> output = template . render ( path , template_values ) <EOL> self . write ( output ) <EOL> class SiteInfoHandler ( ApiHandler ) : <EOL> def get ( self ) : <EOL> site = GetSite ( ) <EOL> template_values = { } <EOL> template_values [ '<STR_LIT>' ] = site <EOL> path = os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> output = template . render ( path , template_values ) <EOL> self . write ( output ) <EOL> class NodesAllHandler ( ApiHandler ) : <EOL> def get ( self ) : <EOL> output = memcache . get ( '<STR_LIT>' ) <EOL> if output is None : <EOL> site = GetSite ( ) <EOL> template_values = { } <EOL> template_values [ '<STR_LIT>' ] = site <EOL> nodes = memcache . get ( '<STR_LIT>' ) <EOL> if nodes is None : <EOL> nodes = db . GqlQuery ( "<STR_LIT>" ) <EOL> memcache . set ( '<STR_LIT>' , nodes , <NUM_LIT> ) <EOL> template_values [ '<STR_LIT>' ] = nodes <EOL> path = os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> output = template . render ( path , template_values ) <EOL> memcache . set ( '<STR_LIT>' , output , <NUM_LIT> ) <EOL> self . write ( output ) <EOL> class NodesShowHandler ( ApiHandler ) : <EOL> def get ( self ) : <EOL> site = GetSite ( ) <EOL> template_values = { } <EOL> template_values [ '<STR_LIT>' ] = site <EOL> method_determined = False <EOL> parameter_id = self . request . get ( '<STR_LIT:id>' ) <EOL> if parameter_id : <EOL> method_determined = True <EOL> if method_determined is not True : <EOL> parameter_name = self . request . get ( '<STR_LIT:name>' ) <EOL> if parameter_name : <EOL> method_determined = True <EOL> if method_determined is True : <EOL> if parameter_id : <EOL> node = GetKindByNum ( '<STR_LIT>' , int ( parameter_id ) ) <EOL> else : <EOL> node = GetKindByName ( '<STR_LIT>' , str ( parameter_name ) ) <EOL> if node is not False : <EOL> template_values [ '<STR_LIT>' ] = node <EOL> path = os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> output = template . render ( path , template_values ) <EOL> self . write ( output ) <EOL> else : <EOL> template_values [ '<STR_LIT:message>' ] = '<STR_LIT>' <EOL> path = os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> output = template . render ( path , template_values ) <EOL> self . write ( output ) <EOL> else : <EOL> template_values [ '<STR_LIT:message>' ] = "<STR_LIT>" <EOL> path = os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> output = template . render ( path , template_values ) <EOL> self . write ( output ) <EOL> class TopicsLatestHandler ( ApiHandler ) : <EOL> def get ( self ) : <EOL> site = GetSite ( ) <EOL> template_values = { } <EOL> template_values [ '<STR_LIT>' ] = site <EOL> topics = memcache . get ( '<STR_LIT>' ) <EOL> if topics is None : <EOL> topics = db . GqlQuery ( "<STR_LIT>" ) <EOL> memcache . set ( '<STR_LIT>' , topics , <NUM_LIT> ) <EOL> template_values [ '<STR_LIT>' ] = topics <EOL> template_values [ '<STR_LIT>' ] = topics . count ( ) <EOL> path = os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> output = template . render ( path , template_values ) <EOL> self . write ( output ) <EOL> class TopicsShowHandler ( ApiHandler ) : <EOL> def get ( self ) : <EOL> site = GetSite ( ) <EOL> template_values = { } <EOL> template_values [ '<STR_LIT>' ] = site <EOL> method_determined = False <EOL> parameter_id = self . request . get ( '<STR_LIT:id>' ) <EOL> parameter_username = False <EOL> parameter_node_id = False <EOL> parameter_node_name = False <EOL> if parameter_id : <EOL> method_determined = True <EOL> if method_determined is False : <EOL> parameter_username = self . request . get ( '<STR_LIT:username>' ) <EOL> if parameter_username : <EOL> method_determined = True <EOL> if method_determined is False : <EOL> parameter_node_id = self . request . get ( '<STR_LIT>' ) <EOL> if parameter_node_id : <EOL> method_determined = True <EOL> if method_determined is False : <EOL> parameter_node_name = self . request . get ( '<STR_LIT>' ) <EOL> if parameter_node_name : <EOL> method_determined = True <EOL> if method_determined is False : <EOL> template_values [ '<STR_LIT:message>' ] = "<STR_LIT>" <EOL> path = os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> output = template . render ( path , template_values ) <EOL> self . response . set_status ( <NUM_LIT> , '<STR_LIT>' ) <EOL> self . write ( output ) <EOL> else : <EOL> topics = False <EOL> topic = False <EOL> if parameter_id : <EOL> try : <EOL> topic = GetKindByNum ( '<STR_LIT>' , int ( parameter_id ) ) <EOL> if topic is not False : <EOL> topics = [ ] <EOL> topics . append ( topic ) <EOL> template_values [ '<STR_LIT>' ] = topic <EOL> except : <EOL> topics = False <EOL> if topics is False : <EOL> if parameter_username : <EOL> one = GetMemberByUsername ( parameter_username ) <EOL> if one is not False : <EOL> topics = db . GqlQuery ( "<STR_LIT>" , one . num ) <EOL> template_values [ '<STR_LIT>' ] = topics <EOL> if topics is False : <EOL> try : <EOL> if parameter_node_id : <EOL> node = GetKindByNum ( '<STR_LIT>' , int ( parameter_node_id ) ) <EOL> if node is not False : <EOL> topics = db . GqlQuery ( "<STR_LIT>" , node . num ) <EOL> template_values [ '<STR_LIT>' ] = topics <EOL> except : <EOL> topics = False <EOL> if topics is False : <EOL> if parameter_node_name : <EOL> node = GetKindByName ( '<STR_LIT>' , str ( parameter_node_name ) ) <EOL> if node is not False : <EOL> topics = db . GqlQuery ( "<STR_LIT>" , node . num ) <EOL> template_values [ '<STR_LIT>' ] = topics <EOL> if topic or topics : <EOL> path = os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> output = template . render ( path , template_values ) <EOL> self . write ( output ) <EOL> else : <EOL> template_values [ '<STR_LIT:message>' ] = "<STR_LIT>" <EOL> path = os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> output = template . render ( path , template_values ) <EOL> self . response . set_status ( <NUM_LIT> , '<STR_LIT>' ) <EOL> self . write ( output ) <EOL> class TopicsCreateHandler ( webapp . RequestHandler ) : <EOL> def post ( self ) : <EOL> authenticated = False <EOL> if '<STR_LIT>' in self . request . headers : <EOL> auth = self . request . headers [ '<STR_LIT>' ] <EOL> decoded = base64 . b64decode ( auth [ <NUM_LIT:6> : ] ) <EOL> authenticated = True <EOL> if authenticated : <EOL> self . response . out . write ( '<STR_LIT:OK>' ) <EOL> else : <EOL> site = GetSite ( ) <EOL> template_values = { } <EOL> template_values [ '<STR_LIT>' ] = site <EOL> template_values [ '<STR_LIT:message>' ] = "<STR_LIT>" <EOL> path = os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> output = template . render ( path , template_values ) <EOL> self . response . set_status ( <NUM_LIT> , '<STR_LIT>' ) <EOL> self . response . headers [ '<STR_LIT>' ] = '<STR_LIT:application/json>' <EOL> self . response . headers [ '<STR_LIT>' ] = '<STR_LIT>' + site . domain + '<STR_LIT:">' <EOL> self . response . out . write ( output ) <EOL> class RepliesShowHandler ( ApiHandler ) : <EOL> def get ( self ) : <EOL> site = GetSite ( ) <EOL> template_values = { } <EOL> template_values [ '<STR_LIT>' ] = site <EOL> method_determined = False <EOL> topic_id = self . request . get ( '<STR_LIT>' ) <EOL> page = self . request . get ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> page_size = TOPIC_PAGE_SIZE <EOL> if topic_id : <EOL> page_start = ( int ( page ) - <NUM_LIT:1> ) * page_size <EOL> replies = db . GqlQuery ( "<STR_LIT>" + str ( page_start ) + "<STR_LIT:U+002C>" + str ( page_size ) , int ( topic_id ) ) <EOL> if replies : <EOL> path = os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> template_values [ '<STR_LIT>' ] = replies <EOL> output = template . render ( path , template_values ) <EOL> self . write ( output ) <EOL> else : <EOL> template_values [ '<STR_LIT:message>' ] = "<STR_LIT>" <EOL> path = os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> output = template . render ( path , template_values ) <EOL> self . response . set_status ( <NUM_LIT> , '<STR_LIT>' ) <EOL> self . write ( output ) <EOL> else : <EOL> template_values [ '<STR_LIT:message>' ] = "<STR_LIT>" <EOL> path = os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> output = template . render ( path , template_values ) <EOL> self . response . set_status ( <NUM_LIT> , '<STR_LIT>' ) <EOL> self . write ( output ) <EOL> class MembersShowHandler ( ApiHandler ) : <EOL> def get ( self ) : <EOL> site = GetSite ( ) <EOL> template_values = { } <EOL> template_values [ '<STR_LIT>' ] = site <EOL> username = self . request . get ( '<STR_LIT:username>' ) <EOL> if username : <EOL> one = GetMemberByUsername ( username ) <EOL> if one is not False : <EOL> if one . avatar_mini_url : <EOL> if ( one . avatar_mini_url [ <NUM_LIT:0> : <NUM_LIT:1> ] == '<STR_LIT:/>' ) : <EOL> one . avatar_mini_url = '<STR_LIT>' + site . domain + one . avatar_mini_url <EOL> one . avatar_normal_url = '<STR_LIT>' + site . domain + one . avatar_normal_url <EOL> one . avatar_large_url = '<STR_LIT>' + site . domain + one . avatar_large_url <EOL> template_values [ '<STR_LIT>' ] = one <EOL> path = os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> output = template . render ( path , template_values ) <EOL> self . write ( output ) <EOL> else : <EOL> template_values [ '<STR_LIT:message>' ] = "<STR_LIT>" <EOL> path = os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> output = template . render ( path , template_values ) <EOL> self . response . set_status ( <NUM_LIT> , '<STR_LIT>' ) <EOL> self . write ( output ) <EOL> else : <EOL> template_values [ '<STR_LIT:message>' ] = "<STR_LIT>" <EOL> path = os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> output = template . render ( path , template_values ) <EOL> self . response . set_status ( <NUM_LIT> , '<STR_LIT>' ) <EOL> self . write ( output ) <EOL> class CurrencyHandler ( ApiHandler ) : <EOL> def get ( self ) : <EOL> codes = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> template_values = { } <EOL> o = memcache . get ( '<STR_LIT>' ) <EOL> if o is not None : <EOL> pass <EOL> else : <EOL> for code in codes : <EOL> url = '<STR_LIT>' + code <EOL> response = urlfetch . fetch ( url ) <EOL> m = re . findall ( '<STR_LIT>' , response . content ) <EOL> if len ( m ) > <NUM_LIT:0> : <EOL> value = m [ <NUM_LIT:0> ] . strip ( ) . replace ( '<STR_LIT:U+0020>' , '<STR_LIT>' ) <EOL> else : <EOL> value = <NUM_LIT:0> <EOL> template_values [ code . lower ( ) ] = value <EOL> path = os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> o = template . render ( path , template_values ) <EOL> memcache . set ( '<STR_LIT>' , o , <NUM_LIT> ) <EOL> self . write ( o ) <EOL> def main ( ) : <EOL> application = webapp . WSGIApplication ( [ <EOL> ( '<STR_LIT>' , SiteStatsHandler ) , <EOL> ( '<STR_LIT>' , SiteInfoHandler ) , <EOL> ( '<STR_LIT>' , NodesAllHandler ) , <EOL> ( '<STR_LIT>' , NodesShowHandler ) , <EOL> ( '<STR_LIT>' , TopicsLatestHandler ) , <EOL> ( '<STR_LIT>' , TopicsShowHandler ) , <EOL> ( '<STR_LIT>' , TopicsCreateHandler ) , <EOL> ( '<STR_LIT>' , RepliesShowHandler ) , <EOL> ( '<STR_LIT>' , MembersShowHandler ) , <EOL> ( '<STR_LIT>' , CurrencyHandler ) <EOL> ] , <EOL> debug = True ) <EOL> util . run_wsgi_app ( application ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
<s> import sys <EOL> import os <EOL> import glob <EOL> import unittest <EOL> def buildTestSuite ( ) : <EOL> suite = unittest . TestSuite ( ) <EOL> for testcase in glob . glob ( '<STR_LIT>' ) : <EOL> module = os . path . splitext ( testcase ) [ <NUM_LIT:0> ] <EOL> suite . addTest ( __import__ ( module ) . buildTestSuite ( ) ) <EOL> return suite <EOL> def main ( ) : <EOL> results = unittest . TextTestRunner ( ) . run ( buildTestSuite ( ) ) <EOL> return results <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> results = main ( ) <EOL> if not results . wasSuccessful ( ) : <EOL> sys . exit ( <NUM_LIT:1> ) </s>
<s> from xml . dom . pulldom import START_ELEMENT , END_ELEMENT , COMMENT , IGNORABLE_WHITESPACE , CHARACTERS <EOL> import _base <EOL> from html5lib . constants import voidElements <EOL> class TreeWalker ( _base . TreeWalker ) : <EOL> def __iter__ ( self ) : <EOL> ignore_until = None <EOL> previous = None <EOL> for event in self . tree : <EOL> if previous is not None and ( ignore_until is None or previous [ <NUM_LIT:1> ] is ignore_until ) : <EOL> if previous [ <NUM_LIT:1> ] is ignore_until : <EOL> ignore_until = None <EOL> for token in self . tokens ( previous , event ) : <EOL> yield token <EOL> if token [ "<STR_LIT:type>" ] == "<STR_LIT>" : <EOL> ignore_until = previous [ <NUM_LIT:1> ] <EOL> previous = event <EOL> if ignore_until is None or previous [ <NUM_LIT:1> ] is ignore_until : <EOL> for token in self . tokens ( previous , None ) : <EOL> yield token <EOL> elif ignore_until is not None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> def tokens ( self , event , next ) : <EOL> type , node = event <EOL> if type == START_ELEMENT : <EOL> name = node . nodeName <EOL> namespace = node . namespaceURI <EOL> attrs = { } <EOL> for attr in node . attributes . keys ( ) : <EOL> attr = node . getAttributeNode ( attr ) <EOL> attrs [ ( attr . namespaceURI , attr . localName ) ] = attr . value <EOL> if name in voidElements : <EOL> for token in self . emptyTag ( namespace , <EOL> name , <EOL> attrs , <EOL> not next or next [ <NUM_LIT:1> ] is not node ) : <EOL> yield token <EOL> else : <EOL> yield self . startTag ( namespace , name , attrs ) <EOL> elif type == END_ELEMENT : <EOL> name = node . nodeName <EOL> namespace = node . namespaceURI <EOL> if name not in voidElements : <EOL> yield self . endTag ( namespace , name ) <EOL> elif type == COMMENT : <EOL> yield self . comment ( node . nodeValue ) <EOL> elif type in ( IGNORABLE_WHITESPACE , CHARACTERS ) : <EOL> for token in self . text ( node . nodeValue ) : <EOL> yield token <EOL> else : <EOL> yield self . unknown ( type ) </s>
<s> """<STR_LIT>""" <EOL> import re <EOL> try : <EOL> from mapreduce . lib . simplejson . _speedups import make_scanner as c_make_scanner <EOL> except ImportError : <EOL> c_make_scanner = None <EOL> __all__ = [ '<STR_LIT>' ] <EOL> NUMBER_RE = re . compile ( <EOL> r'<STR_LIT>' , <EOL> ( re . VERBOSE | re . MULTILINE | re . DOTALL ) ) <EOL> def py_make_scanner ( context ) : <EOL> parse_object = context . parse_object <EOL> parse_array = context . parse_array <EOL> parse_string = context . parse_string <EOL> match_number = NUMBER_RE . match <EOL> encoding = context . encoding <EOL> strict = context . strict <EOL> parse_float = context . parse_float <EOL> parse_int = context . parse_int <EOL> parse_constant = context . parse_constant <EOL> object_hook = context . object_hook <EOL> def _scan_once ( string , idx ) : <EOL> try : <EOL> nextchar = string [ idx ] <EOL> except IndexError : <EOL> raise StopIteration <EOL> if nextchar == '<STR_LIT:">' : <EOL> return parse_string ( string , idx + <NUM_LIT:1> , encoding , strict ) <EOL> elif nextchar == '<STR_LIT:{>' : <EOL> return parse_object ( ( string , idx + <NUM_LIT:1> ) , encoding , strict , _scan_once , object_hook ) <EOL> elif nextchar == '<STR_LIT:[>' : <EOL> return parse_array ( ( string , idx + <NUM_LIT:1> ) , _scan_once ) <EOL> elif nextchar == '<STR_LIT:n>' and string [ idx : idx + <NUM_LIT:4> ] == '<STR_LIT:null>' : <EOL> return None , idx + <NUM_LIT:4> <EOL> elif nextchar == '<STR_LIT:t>' and string [ idx : idx + <NUM_LIT:4> ] == '<STR_LIT:true>' : <EOL> return True , idx + <NUM_LIT:4> <EOL> elif nextchar == '<STR_LIT:f>' and string [ idx : idx + <NUM_LIT:5> ] == '<STR_LIT:false>' : <EOL> return False , idx + <NUM_LIT:5> <EOL> m = match_number ( string , idx ) <EOL> if m is not None : <EOL> integer , frac , exp = m . groups ( ) <EOL> if frac or exp : <EOL> res = parse_float ( integer + ( frac or '<STR_LIT>' ) + ( exp or '<STR_LIT>' ) ) <EOL> else : <EOL> res = parse_int ( integer ) <EOL> return res , m . end ( ) <EOL> elif nextchar == '<STR_LIT:N>' and string [ idx : idx + <NUM_LIT:3> ] == '<STR_LIT>' : <EOL> return parse_constant ( '<STR_LIT>' ) , idx + <NUM_LIT:3> <EOL> elif nextchar == '<STR_LIT:I>' and string [ idx : idx + <NUM_LIT:8> ] == '<STR_LIT>' : <EOL> return parse_constant ( '<STR_LIT>' ) , idx + <NUM_LIT:8> <EOL> elif nextchar == '<STR_LIT:->' and string [ idx : idx + <NUM_LIT:9> ] == '<STR_LIT>' : <EOL> return parse_constant ( '<STR_LIT>' ) , idx + <NUM_LIT:9> <EOL> else : <EOL> raise StopIteration <EOL> return _scan_once <EOL> make_scanner = c_make_scanner or py_make_scanner </s>
<s> import hashlib <EOL> import logging <EOL> import zlib <EOL> import pickle <EOL> from google . appengine . ext import db <EOL> from google . appengine . api import memcache <EOL> from v2ex . babel import Member <EOL> from v2ex . babel import Counter <EOL> from v2ex . babel import Section <EOL> from v2ex . babel import Node <EOL> from v2ex . babel import Topic <EOL> from v2ex . babel import Reply <EOL> from v2ex . babel import Place <EOL> from v2ex . babel import Site <EOL> def GetKindByNum ( kind , num ) : <EOL> K = str ( kind . capitalize ( ) ) <EOL> one = memcache . get ( K + '<STR_LIT:_>' + str ( num ) ) <EOL> if one : <EOL> return one <EOL> else : <EOL> q = db . GqlQuery ( "<STR_LIT>" + K + "<STR_LIT>" , int ( num ) ) <EOL> if q . count ( ) == <NUM_LIT:1> : <EOL> one = q [ <NUM_LIT:0> ] <EOL> memcache . set ( K + '<STR_LIT:_>' + str ( num ) , one , <NUM_LIT> ) <EOL> return one <EOL> else : <EOL> return False <EOL> def GetKindByName ( kind , name ) : <EOL> K = str ( kind . capitalize ( ) ) <EOL> one = memcache . get ( K + '<STR_LIT>' + str ( name ) ) <EOL> if one : <EOL> return one <EOL> else : <EOL> q = db . GqlQuery ( "<STR_LIT>" + K + "<STR_LIT>" , str ( name ) ) <EOL> if q . count ( ) == <NUM_LIT:1> : <EOL> one = q [ <NUM_LIT:0> ] <EOL> memcache . set ( K + '<STR_LIT>' + str ( name ) , one , <NUM_LIT> ) <EOL> return one <EOL> else : <EOL> return False <EOL> def GetMemberByUsername ( name ) : <EOL> one = memcache . get ( '<STR_LIT>' + str ( name ) . lower ( ) ) <EOL> if one : <EOL> return one <EOL> else : <EOL> q = db . GqlQuery ( "<STR_LIT>" , str ( name ) . lower ( ) ) <EOL> if q . count ( ) == <NUM_LIT:1> : <EOL> one = q [ <NUM_LIT:0> ] <EOL> memcache . set ( '<STR_LIT>' + str ( name ) . lower ( ) , one , <NUM_LIT> ) <EOL> return one <EOL> else : <EOL> return False <EOL> def GetMemberByEmail ( email ) : <EOL> cache = '<STR_LIT>' + hashlib . md5 ( email . lower ( ) ) . hexdigest ( ) <EOL> one = memcache . get ( cache ) <EOL> if one : <EOL> return one <EOL> else : <EOL> q = db . GqlQuery ( "<STR_LIT>" , str ( email ) . lower ( ) ) <EOL> if q . count ( ) == <NUM_LIT:1> : <EOL> one = q [ <NUM_LIT:0> ] <EOL> memcache . set ( cache , one , <NUM_LIT> ) <EOL> return one <EOL> else : <EOL> return False <EOL> def ip2long ( ip ) : <EOL> ip_array = ip . split ( '<STR_LIT:.>' ) <EOL> ip_long = int ( ip_array [ <NUM_LIT:0> ] ) * <NUM_LIT> + int ( ip_array [ <NUM_LIT:1> ] ) * <NUM_LIT> + int ( ip_array [ <NUM_LIT:2> ] ) * <NUM_LIT> + int ( ip_array [ <NUM_LIT:3> ] ) <EOL> return ip_long <EOL> def GetPlaceByIP ( ip ) : <EOL> cache = '<STR_LIT>' + ip <EOL> place = memcache . get ( cache ) <EOL> if place : <EOL> return place <EOL> else : <EOL> q = db . GqlQuery ( "<STR_LIT>" , ip ) <EOL> if q . count ( ) == <NUM_LIT:1> : <EOL> place = q [ <NUM_LIT:0> ] <EOL> memcache . set ( cache , place , <NUM_LIT> ) <EOL> return place <EOL> else : <EOL> return False <EOL> def CreatePlaceByIP ( ip ) : <EOL> place = Place ( ) <EOL> q = db . GqlQuery ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if ( q . count ( ) == <NUM_LIT:1> ) : <EOL> counter = q [ <NUM_LIT:0> ] <EOL> counter . value = counter . value + <NUM_LIT:1> <EOL> else : <EOL> counter = Counter ( ) <EOL> counter . name = '<STR_LIT>' <EOL> counter . value = <NUM_LIT:1> <EOL> q2 = db . GqlQuery ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if ( q2 . count ( ) == <NUM_LIT:1> ) : <EOL> counter2 = q2 [ <NUM_LIT:0> ] <EOL> counter2 . value = counter2 . value + <NUM_LIT:1> <EOL> else : <EOL> counter2 = Counter ( ) <EOL> counter2 . name = '<STR_LIT>' <EOL> counter2 . value = <NUM_LIT:1> <EOL> place . num = ip2long ( ip ) <EOL> place . ip = ip <EOL> place . put ( ) <EOL> counter . put ( ) <EOL> counter2 . put ( ) <EOL> return place <EOL> def GetSite ( ) : <EOL> site = memcache . get ( '<STR_LIT>' ) <EOL> if site is not None : <EOL> return site <EOL> else : <EOL> q = db . GqlQuery ( "<STR_LIT>" ) <EOL> if q . count ( ) == <NUM_LIT:1> : <EOL> site = q [ <NUM_LIT:0> ] <EOL> if site . l10n is None : <EOL> site . l10n = '<STR_LIT>' <EOL> if site . meta is None : <EOL> site . meta = '<STR_LIT>' <EOL> memcache . set ( '<STR_LIT>' , site , <NUM_LIT> ) <EOL> return site <EOL> else : <EOL> site = Site ( ) <EOL> site . num = <NUM_LIT:1> <EOL> site . title = '<STR_LIT>' <EOL> site . domain = '<STR_LIT>' <EOL> site . slogan = '<STR_LIT>' <EOL> site . l10n = '<STR_LIT>' <EOL> site . description = '<STR_LIT>' <EOL> site . meta = '<STR_LIT>' <EOL> site . put ( ) <EOL> memcache . set ( '<STR_LIT>' , site , <NUM_LIT> ) <EOL> return site <EOL> def GetUnpacked ( data ) : <EOL> decompressed = zlib . decompress ( data ) <EOL> return pickle . loads ( decompressed ) <EOL> def GetPacked ( data ) : <EOL> s = pickle . dumps ( data ) <EOL> return zlib . compress ( s ) </s>
<s> from django . utils . six . moves . builtins import str <EOL> import re <EOL> from xml . sax . saxutils import XMLGenerator <EOL> from django . utils import timezone <EOL> GENERATOR_TEXT = '<STR_LIT>' <EOL> GENERATOR_ATTR = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:version>' : '<STR_LIT>' <EOL> } <EOL> class SimplerXMLGenerator ( XMLGenerator ) : <EOL> def addQuickElement ( self , name , contents = None , attrs = None ) : <EOL> "<STR_LIT>" <EOL> if attrs is None : <EOL> attrs = { } <EOL> self . startElement ( name , attrs ) <EOL> if contents is not None : <EOL> self . characters ( contents ) <EOL> self . endElement ( name ) <EOL> def rfc3339_date ( date ) : <EOL> return '<STR_LIT>' % ( date . year , date . month , date . day , date . hour , date . minute , date . second ) <EOL> def get_tag_uri ( url , date ) : <EOL> "<STR_LIT>" <EOL> tag = re . sub ( '<STR_LIT>' , '<STR_LIT>' , url ) <EOL> if date is not None : <EOL> tag = re . sub ( '<STR_LIT:/>' , '<STR_LIT>' % ( date . year , date . month , date . day ) , tag , <NUM_LIT:1> ) <EOL> tag = re . sub ( '<STR_LIT:#>' , '<STR_LIT:/>' , tag ) <EOL> return '<STR_LIT>' + tag <EOL> class Feed ( object ) : <EOL> VALIDATE = True <EOL> def __init__ ( self , slug , feed_url ) : <EOL> pass <EOL> def __get_dynamic_attr ( self , attname , obj , default = None ) : <EOL> try : <EOL> attr = getattr ( self , attname ) <EOL> except AttributeError : <EOL> return default <EOL> if callable ( attr ) : <EOL> if hasattr ( attr , '<STR_LIT>' ) : <EOL> argcount = attr . func_code . co_argcount <EOL> else : <EOL> argcount = attr . __call__ . func_code . co_argcount <EOL> if argcount == <NUM_LIT:2> : <EOL> return attr ( obj ) <EOL> else : <EOL> return attr ( ) <EOL> return attr <EOL> def get_feed ( self , extra_params = None ) : <EOL> if extra_params : <EOL> try : <EOL> obj = self . get_object ( extra_params . split ( '<STR_LIT:/>' ) ) <EOL> except ( AttributeError , LookupError ) : <EOL> raise LookupError ( '<STR_LIT>' ) <EOL> else : <EOL> obj = None <EOL> feed = AtomFeed ( <EOL> atom_id = self . __get_dynamic_attr ( '<STR_LIT>' , obj ) , <EOL> title = self . __get_dynamic_attr ( '<STR_LIT>' , obj ) , <EOL> updated = self . __get_dynamic_attr ( '<STR_LIT>' , obj ) , <EOL> icon = self . __get_dynamic_attr ( '<STR_LIT>' , obj ) , <EOL> logo = self . __get_dynamic_attr ( '<STR_LIT>' , obj ) , <EOL> rights = self . __get_dynamic_attr ( '<STR_LIT>' , obj ) , <EOL> subtitle = self . __get_dynamic_attr ( '<STR_LIT>' , obj ) , <EOL> authors = self . __get_dynamic_attr ( '<STR_LIT>' , obj , default = [ ] ) , <EOL> categories = self . __get_dynamic_attr ( '<STR_LIT>' , obj , default = [ ] ) , <EOL> contributors = self . __get_dynamic_attr ( '<STR_LIT>' , obj , default = [ ] ) , <EOL> links = self . __get_dynamic_attr ( '<STR_LIT>' , obj , default = [ ] ) , <EOL> extra_attrs = self . __get_dynamic_attr ( '<STR_LIT>' , obj ) , <EOL> hide_generator = self . __get_dynamic_attr ( '<STR_LIT>' , obj , default = False ) <EOL> ) <EOL> items = self . __get_dynamic_attr ( '<STR_LIT>' , obj ) <EOL> if items is None : <EOL> raise LookupError ( '<STR_LIT>' ) <EOL> for item in items : <EOL> feed . add_item ( <EOL> atom_id = self . __get_dynamic_attr ( '<STR_LIT>' , item ) , <EOL> title = self . __get_dynamic_attr ( '<STR_LIT>' , item ) , <EOL> updated = self . __get_dynamic_attr ( '<STR_LIT>' , item ) , <EOL> content = self . __get_dynamic_attr ( '<STR_LIT>' , item ) , <EOL> published = self . __get_dynamic_attr ( '<STR_LIT>' , item ) , <EOL> rights = self . __get_dynamic_attr ( '<STR_LIT>' , item ) , <EOL> source = self . __get_dynamic_attr ( '<STR_LIT>' , item ) , <EOL> summary = self . __get_dynamic_attr ( '<STR_LIT>' , item ) , <EOL> authors = self . __get_dynamic_attr ( '<STR_LIT>' , item , default = [ ] ) , <EOL> categories = self . __get_dynamic_attr ( '<STR_LIT>' , item , default = [ ] ) , <EOL> contributors = self . __get_dynamic_attr ( '<STR_LIT>' , item , default = [ ] ) , <EOL> links = self . __get_dynamic_attr ( '<STR_LIT>' , item , default = [ ] ) , <EOL> extra_attrs = self . __get_dynamic_attr ( '<STR_LIT>' , None , default = { } ) , <EOL> ) <EOL> if self . VALIDATE : <EOL> feed . validate ( ) <EOL> return feed <EOL> class ValidationError ( Exception ) : <EOL> pass <EOL> class AtomFeed ( object ) : <EOL> mime_type = '<STR_LIT>' <EOL> ns = u'<STR_LIT>' <EOL> def __init__ ( self , atom_id , title , updated = None , icon = None , logo = None , rights = None , subtitle = None , authors = None , categories = None , contributors = None , links = None , extra_attrs = { } , hide_generator = False ) : <EOL> if atom_id is None : <EOL> raise LookupError ( '<STR_LIT>' ) <EOL> if title is None : <EOL> raise LookupError ( '<STR_LIT>' ) <EOL> self . feed = { <EOL> '<STR_LIT:id>' : atom_id , <EOL> '<STR_LIT:title>' : title , <EOL> '<STR_LIT>' : updated , <EOL> '<STR_LIT>' : icon , <EOL> '<STR_LIT>' : logo , <EOL> '<STR_LIT>' : rights , <EOL> '<STR_LIT>' : subtitle , <EOL> '<STR_LIT>' : authors or [ ] , <EOL> '<STR_LIT>' : categories or [ ] , <EOL> '<STR_LIT>' : contributors or [ ] , <EOL> '<STR_LIT>' : links or [ ] , <EOL> '<STR_LIT>' : extra_attrs , <EOL> '<STR_LIT>' : hide_generator , <EOL> } <EOL> self . items = [ ] <EOL> def add_item ( self , atom_id , title , updated , content = None , published = None , rights = None , source = None , summary = None , authors = None , categories = None , contributors = None , links = None , extra_attrs = { } ) : <EOL> if atom_id is None : <EOL> raise LookupError ( '<STR_LIT>' ) <EOL> if title is None : <EOL> raise LookupError ( '<STR_LIT>' ) <EOL> if updated is None : <EOL> raise LookupError ( '<STR_LIT>' ) <EOL> self . items . append ( { <EOL> '<STR_LIT:id>' : atom_id , <EOL> '<STR_LIT:title>' : title , <EOL> '<STR_LIT>' : updated , <EOL> '<STR_LIT:content>' : content , <EOL> '<STR_LIT>' : published , <EOL> '<STR_LIT>' : rights , <EOL> '<STR_LIT:source>' : source , <EOL> '<STR_LIT>' : summary , <EOL> '<STR_LIT>' : authors or [ ] , <EOL> '<STR_LIT>' : categories or [ ] , <EOL> '<STR_LIT>' : contributors or [ ] , <EOL> '<STR_LIT>' : links or [ ] , <EOL> '<STR_LIT>' : extra_attrs , <EOL> } ) <EOL> def latest_updated ( self ) : <EOL> """<STR_LIT>""" <EOL> updates = [ item [ '<STR_LIT>' ] for item in self . items ] <EOL> if len ( updates ) > <NUM_LIT:0> : <EOL> updates . sort ( ) <EOL> return updates [ - <NUM_LIT:1> ] <EOL> else : <EOL> return timezone . now ( ) <EOL> def write_text_construct ( self , handler , element_name , data ) : <EOL> if isinstance ( data , tuple ) : <EOL> text_type , text = data <EOL> if text_type == '<STR_LIT>' : <EOL> handler . startElement ( element_name , { '<STR_LIT:type>' : text_type } ) <EOL> handler . _write ( text ) <EOL> handler . endElement ( element_name ) <EOL> else : <EOL> handler . addQuickElement ( element_name , text , { '<STR_LIT:type>' : text_type } ) <EOL> else : <EOL> handler . addQuickElement ( element_name , data ) <EOL> def write_person_construct ( self , handler , element_name , person ) : <EOL> handler . startElement ( element_name , { } ) <EOL> handler . addQuickElement ( u'<STR_LIT:name>' , person [ '<STR_LIT:name>' ] ) <EOL> if '<STR_LIT>' in person : <EOL> handler . addQuickElement ( u'<STR_LIT>' , person [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT:email>' in person : <EOL> handler . addQuickElement ( u'<STR_LIT:email>' , person [ '<STR_LIT:email>' ] ) <EOL> handler . endElement ( element_name ) <EOL> def write_link_construct ( self , handler , link ) : <EOL> if '<STR_LIT>' in link : <EOL> link [ '<STR_LIT>' ] = str ( link [ '<STR_LIT>' ] ) <EOL> handler . addQuickElement ( u'<STR_LIT>' , None , link ) <EOL> def write_category_construct ( self , handler , category ) : <EOL> handler . addQuickElement ( u'<STR_LIT>' , None , category ) <EOL> def write_source ( self , handler , data ) : <EOL> handler . startElement ( u'<STR_LIT:source>' , { } ) <EOL> if data . get ( '<STR_LIT:id>' ) : <EOL> handler . addQuickElement ( u'<STR_LIT:id>' , data [ '<STR_LIT:id>' ] ) <EOL> if data . get ( '<STR_LIT:title>' ) : <EOL> self . write_text_construct ( handler , u'<STR_LIT:title>' , data [ '<STR_LIT:title>' ] ) <EOL> if data . get ( '<STR_LIT>' ) : <EOL> self . write_text_construct ( handler , u'<STR_LIT>' , data [ '<STR_LIT>' ] ) <EOL> if data . get ( '<STR_LIT>' ) : <EOL> handler . addQuickElement ( u'<STR_LIT>' , data [ '<STR_LIT>' ] ) <EOL> if data . get ( '<STR_LIT>' ) : <EOL> handler . addQuickElement ( u'<STR_LIT>' , data [ '<STR_LIT>' ] ) <EOL> if data . get ( '<STR_LIT>' ) : <EOL> handler . addQuickElement ( u'<STR_LIT>' , rfc3339_date ( data [ '<STR_LIT>' ] ) ) <EOL> for category in data . get ( '<STR_LIT>' , [ ] ) : <EOL> self . write_category_construct ( handler , category ) <EOL> for link in data . get ( '<STR_LIT>' , [ ] ) : <EOL> self . write_link_construct ( handler , link ) <EOL> for author in data . get ( '<STR_LIT>' , [ ] ) : <EOL> self . write_person_construct ( handler , u'<STR_LIT>' , author ) <EOL> for contributor in data . get ( '<STR_LIT>' , [ ] ) : <EOL> self . write_person_construct ( handler , u'<STR_LIT>' , contributor ) <EOL> if data . get ( '<STR_LIT>' ) : <EOL> self . write_text_construct ( handler , u'<STR_LIT>' , data [ '<STR_LIT>' ] ) <EOL> handler . endElement ( u'<STR_LIT:source>' ) <EOL> def write_content ( self , handler , data ) : <EOL> if isinstance ( data , tuple ) : <EOL> content_dict , text = data <EOL> if content_dict . get ( '<STR_LIT:type>' ) == '<STR_LIT>' : <EOL> handler . startElement ( u'<STR_LIT:content>' , content_dict ) <EOL> handler . _write ( text ) <EOL> handler . endElement ( u'<STR_LIT:content>' ) <EOL> else : <EOL> handler . addQuickElement ( u'<STR_LIT:content>' , text , content_dict ) <EOL> else : <EOL> handler . addQuickElement ( u'<STR_LIT:content>' , data ) <EOL> def write ( self , outfile , encoding ) : <EOL> handler = SimplerXMLGenerator ( outfile , encoding ) <EOL> handler . startDocument ( ) <EOL> feed_attrs = { u'<STR_LIT>' : self . ns } <EOL> if self . feed . get ( '<STR_LIT>' ) : <EOL> feed_attrs . update ( self . feed [ '<STR_LIT>' ] ) <EOL> handler . startElement ( u'<STR_LIT>' , feed_attrs ) <EOL> handler . addQuickElement ( u'<STR_LIT:id>' , self . feed [ '<STR_LIT:id>' ] ) <EOL> self . write_text_construct ( handler , u'<STR_LIT:title>' , self . feed [ '<STR_LIT:title>' ] ) <EOL> if self . feed . get ( '<STR_LIT>' ) : <EOL> self . write_text_construct ( handler , u'<STR_LIT>' , self . feed [ '<STR_LIT>' ] ) <EOL> if self . feed . get ( '<STR_LIT>' ) : <EOL> handler . addQuickElement ( u'<STR_LIT>' , self . feed [ '<STR_LIT>' ] ) <EOL> if self . feed . get ( '<STR_LIT>' ) : <EOL> handler . addQuickElement ( u'<STR_LIT>' , self . feed [ '<STR_LIT>' ] ) <EOL> if self . feed [ '<STR_LIT>' ] : <EOL> handler . addQuickElement ( u'<STR_LIT>' , rfc3339_date ( self . feed [ '<STR_LIT>' ] ) ) <EOL> else : <EOL> handler . addQuickElement ( u'<STR_LIT>' , rfc3339_date ( self . latest_updated ( ) ) ) <EOL> for category in self . feed [ '<STR_LIT>' ] : <EOL> self . write_category_construct ( handler , category ) <EOL> for link in self . feed [ '<STR_LIT>' ] : <EOL> self . write_link_construct ( handler , link ) <EOL> for author in self . feed [ '<STR_LIT>' ] : <EOL> self . write_person_construct ( handler , u'<STR_LIT>' , author ) <EOL> for contributor in self . feed [ '<STR_LIT>' ] : <EOL> self . write_person_construct ( handler , u'<STR_LIT>' , contributor ) <EOL> if self . feed . get ( '<STR_LIT>' ) : <EOL> self . write_text_construct ( handler , u'<STR_LIT>' , self . feed [ '<STR_LIT>' ] ) <EOL> if not self . feed . get ( '<STR_LIT>' ) : <EOL> handler . addQuickElement ( u'<STR_LIT>' , GENERATOR_TEXT , GENERATOR_ATTR ) <EOL> self . write_items ( handler ) <EOL> handler . endElement ( u'<STR_LIT>' ) <EOL> def write_items ( self , handler ) : <EOL> for item in self . items : <EOL> entry_attrs = item . get ( '<STR_LIT>' , { } ) <EOL> handler . startElement ( u'<STR_LIT>' , entry_attrs ) <EOL> handler . addQuickElement ( u'<STR_LIT:id>' , item [ '<STR_LIT:id>' ] ) <EOL> self . write_text_construct ( handler , u'<STR_LIT:title>' , item [ '<STR_LIT:title>' ] ) <EOL> handler . addQuickElement ( u'<STR_LIT>' , rfc3339_date ( item [ '<STR_LIT>' ] ) ) <EOL> if item . get ( '<STR_LIT>' ) : <EOL> handler . addQuickElement ( u'<STR_LIT>' , rfc3339_date ( item [ '<STR_LIT>' ] ) ) <EOL> if item . get ( '<STR_LIT>' ) : <EOL> self . write_text_construct ( handler , u'<STR_LIT>' , item [ '<STR_LIT>' ] ) <EOL> if item . get ( '<STR_LIT:source>' ) : <EOL> self . write_source ( handler , item [ '<STR_LIT:source>' ] ) <EOL> for author in item [ '<STR_LIT>' ] : <EOL> self . write_person_construct ( handler , u'<STR_LIT>' , author ) <EOL> for contributor in item [ '<STR_LIT>' ] : <EOL> self . write_person_construct ( handler , u'<STR_LIT>' , contributor ) <EOL> for category in item [ '<STR_LIT>' ] : <EOL> self . write_category_construct ( handler , category ) <EOL> for link in item [ '<STR_LIT>' ] : <EOL> self . write_link_construct ( handler , link ) <EOL> if item . get ( '<STR_LIT>' ) : <EOL> self . write_text_construct ( handler , u'<STR_LIT>' , item [ '<STR_LIT>' ] ) <EOL> if item . get ( '<STR_LIT:content>' ) : <EOL> self . write_content ( handler , item [ '<STR_LIT:content>' ] ) <EOL> handler . endElement ( u'<STR_LIT>' ) <EOL> def validate ( self ) : <EOL> def validate_text_construct ( obj ) : <EOL> if isinstance ( obj , tuple ) : <EOL> if obj [ <NUM_LIT:0> ] not in [ '<STR_LIT:text>' , '<STR_LIT:html>' , '<STR_LIT>' ] : <EOL> return False <EOL> return True <EOL> if not validate_text_construct ( self . feed [ '<STR_LIT:title>' ] ) : <EOL> raise ValidationError ( '<STR_LIT>' ) <EOL> if self . feed . get ( '<STR_LIT>' ) : <EOL> if not validate_text_construct ( self . feed [ '<STR_LIT>' ] ) : <EOL> raise ValidationError ( '<STR_LIT>' ) <EOL> if self . feed . get ( '<STR_LIT>' ) : <EOL> if not validate_text_construct ( self . feed [ '<STR_LIT>' ] ) : <EOL> raise ValidationError ( '<STR_LIT>' ) <EOL> alternate_links = { } <EOL> for link in self . feed . get ( '<STR_LIT>' ) : <EOL> if link . get ( '<STR_LIT>' ) == '<STR_LIT>' or link . get ( '<STR_LIT>' ) is None : <EOL> key = ( link . get ( '<STR_LIT:type>' ) , link . get ( '<STR_LIT>' ) ) <EOL> if key in alternate_links : <EOL> raise ValidationError ( '<STR_LIT>' ) <EOL> alternate_links [ key ] = link <EOL> if self . feed . get ( '<STR_LIT>' ) : <EOL> feed_author = True <EOL> else : <EOL> feed_author = False <EOL> for item in self . items : <EOL> if not feed_author and not item . get ( '<STR_LIT>' ) : <EOL> if item . get ( '<STR_LIT:source>' ) and item [ '<STR_LIT:source>' ] . get ( '<STR_LIT>' ) : <EOL> pass <EOL> else : <EOL> raise ValidationError ( '<STR_LIT>' ) <EOL> if not validate_text_construct ( item [ '<STR_LIT:title>' ] ) : <EOL> raise ValidationError ( '<STR_LIT>' ) <EOL> if item . get ( '<STR_LIT>' ) : <EOL> if not validate_text_construct ( item [ '<STR_LIT>' ] ) : <EOL> raise ValidationError ( '<STR_LIT>' ) <EOL> if item . get ( '<STR_LIT>' ) : <EOL> if not validate_text_construct ( item [ '<STR_LIT>' ] ) : <EOL> raise ValidationError ( '<STR_LIT>' ) <EOL> source = item . get ( '<STR_LIT:source>' ) <EOL> if source : <EOL> if source . get ( '<STR_LIT:title>' ) : <EOL> if not validate_text_construct ( source [ '<STR_LIT:title>' ] ) : <EOL> raise ValidationError ( '<STR_LIT>' ) <EOL> if source . get ( '<STR_LIT>' ) : <EOL> if not validate_text_construct ( source [ '<STR_LIT>' ] ) : <EOL> raise ValidationError ( '<STR_LIT>' ) <EOL> if source . get ( '<STR_LIT>' ) : <EOL> if not validate_text_construct ( source [ '<STR_LIT>' ] ) : <EOL> raise ValidationError ( '<STR_LIT>' ) <EOL> alternate_links = { } <EOL> for link in item . get ( '<STR_LIT>' ) : <EOL> if link . get ( '<STR_LIT>' ) == '<STR_LIT>' or link . get ( '<STR_LIT>' ) is None : <EOL> key = ( link . get ( '<STR_LIT:type>' ) , link . get ( '<STR_LIT>' ) ) <EOL> if key in alternate_links : <EOL> raise ValidationError ( '<STR_LIT>' ) <EOL> alternate_links [ key ] = link <EOL> if not item . get ( '<STR_LIT:content>' ) : <EOL> if not alternate_links : <EOL> raise ValidationError ( '<STR_LIT>' ) <EOL> if item . get ( '<STR_LIT:content>' ) and isinstance ( item . get ( '<STR_LIT:content>' ) , tuple ) : <EOL> content_type = item . get ( '<STR_LIT:content>' ) [ <NUM_LIT:0> ] . get ( '<STR_LIT:type>' ) <EOL> if item . get ( '<STR_LIT:content>' ) [ <NUM_LIT:0> ] . get ( '<STR_LIT:src>' ) : <EOL> if item . get ( '<STR_LIT:content>' ) [ <NUM_LIT:1> ] : <EOL> raise ValidationError ( '<STR_LIT>' ) <EOL> if not item . get ( '<STR_LIT>' ) : <EOL> raise ValidationError ( '<STR_LIT>' ) <EOL> if content_type in [ '<STR_LIT:text>' , '<STR_LIT:html>' , '<STR_LIT>' ] : <EOL> raise ValidationError ( '<STR_LIT>' ) <EOL> if content_type : <EOL> if '<STR_LIT:/>' in content_type and not content_type . startswith ( '<STR_LIT>' ) and not content_type . endswith ( '<STR_LIT>' ) and not content_type . endswith ( '<STR_LIT>' ) and content_type not in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if not item . get ( '<STR_LIT>' ) : <EOL> raise ValidationError ( '<STR_LIT>' ) <EOL> if content_type not in [ '<STR_LIT:text>' , '<STR_LIT:html>' , '<STR_LIT>' ] and '<STR_LIT:/>' not in content_type : <EOL> raise ValidationError ( '<STR_LIT>' ) <EOL> return <EOL> return <EOL> class LegacySyndicationFeed ( AtomFeed ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , title , link , description , language = None , author_email = None , <EOL> author_name = None , author_link = None , subtitle = None , categories = None , <EOL> feed_url = None , feed_copyright = None ) : <EOL> atom_id = link <EOL> title = title <EOL> updated = None <EOL> rights = feed_copyright <EOL> subtitle = subtitle <EOL> author_dict = { '<STR_LIT:name>' : author_name } <EOL> if author_link : <EOL> author_dict [ '<STR_LIT>' ] = author_link <EOL> if author_email : <EOL> author_dict [ '<STR_LIT:email>' ] = author_email <EOL> authors = [ author_dict ] <EOL> if categories : <EOL> categories = [ { '<STR_LIT>' : term } for term in categories ] <EOL> links = [ { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : link } ] <EOL> if feed_url : <EOL> links . append ( { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : feed_url } ) <EOL> if language : <EOL> extra_attrs = { '<STR_LIT>' : language } <EOL> else : <EOL> extra_attrs = { } <EOL> AtomFeed . __init__ ( self , atom_id , title , updated , rights = rights , subtitle = subtitle , <EOL> authors = authors , categories = categories , links = links , extra_attrs = extra_attrs ) <EOL> def add_item ( self , title , link , description , author_email = None , <EOL> author_name = None , author_link = None , pubdate = None , comments = None , <EOL> unique_id = None , enclosure = None , categories = None , item_copyright = None ) : <EOL> if unique_id : <EOL> atom_id = unique_id <EOL> else : <EOL> atom_id = get_tag_uri ( link , pubdate ) <EOL> title = title <EOL> updated = pubdate <EOL> if item_copyright : <EOL> rights = item_copyright <EOL> else : <EOL> rights = None <EOL> if description : <EOL> summary = '<STR_LIT:html>' , description <EOL> else : <EOL> summary = None <EOL> author_dict = { '<STR_LIT:name>' : author_name } <EOL> if author_link : <EOL> author_dict [ '<STR_LIT>' ] = author_link <EOL> if author_email : <EOL> author_dict [ '<STR_LIT:email>' ] = author_email <EOL> authors = [ author_dict ] <EOL> categories = [ { '<STR_LIT>' : term } for term in categories ] <EOL> links = [ { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : link } ] <EOL> if enclosure : <EOL> links . append ( { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : enclosure . url , '<STR_LIT>' : enclosure . length , '<STR_LIT:type>' : enclosure . mime_type } ) <EOL> AtomFeed . add_item ( self , atom_id , title , updated , rights = rights , summary = summary , <EOL> authors = authors , categories = categories , links = links ) </s>
<s> import datetime <EOL> import pytz <EOL> from django . test import TestCase <EOL> from django . utils . html import escape <EOL> from schedule . models import Event , Rule , Calendar <EOL> from schedule . periods import Period , Day <EOL> from schedule . templatetags . scheduletags import querystring_for_date , prev_url , next_url , create_event_url , _cook_slots <EOL> class TestTemplateTags ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . day = Day ( events = Event . objects . all ( ) , <EOL> date = datetime . datetime ( datetime . datetime . now ( ) . year , <NUM_LIT:2> , <NUM_LIT:7> , <NUM_LIT:0> , <NUM_LIT:0> , tzinfo = pytz . utc ) ) <EOL> self . day_out_of_limit = Day ( events = Event . objects . all ( ) , <EOL> date = datetime . datetime ( datetime . datetime . now ( ) . year + <NUM_LIT:3> , <NUM_LIT:2> , <NUM_LIT:7> , <NUM_LIT:0> , <NUM_LIT:0> , tzinfo = pytz . utc ) ) <EOL> self . day_out_of_limit_lower = Day ( events = Event . objects . all ( ) , <EOL> date = datetime . datetime ( datetime . datetime . now ( ) . year - <NUM_LIT:3> , <NUM_LIT:2> , <NUM_LIT:7> , <NUM_LIT:0> , <NUM_LIT:0> , tzinfo = pytz . utc ) ) <EOL> rule = Rule ( frequency = '<STR_LIT>' ) <EOL> rule . save ( ) <EOL> self . cal = Calendar ( name = '<STR_LIT>' , slug = '<STR_LIT>' ) <EOL> self . cal . save ( ) <EOL> data = { <EOL> '<STR_LIT:title>' : '<STR_LIT>' , <EOL> '<STR_LIT:start>' : datetime . datetime ( datetime . datetime . now ( ) . year , <NUM_LIT:1> , <NUM_LIT:5> , <NUM_LIT:8> , <NUM_LIT:0> , tzinfo = pytz . utc ) , <EOL> '<STR_LIT:end>' : datetime . datetime ( datetime . datetime . now ( ) . year , <NUM_LIT:1> , <NUM_LIT:5> , <NUM_LIT:9> , <NUM_LIT:0> , tzinfo = pytz . utc ) , <EOL> '<STR_LIT>' : datetime . datetime ( datetime . datetime . now ( ) . year , <NUM_LIT:5> , <NUM_LIT:5> , <NUM_LIT:0> , <NUM_LIT:0> , tzinfo = pytz . utc ) , <EOL> '<STR_LIT>' : rule , <EOL> '<STR_LIT>' : self . cal , <EOL> } <EOL> recurring_event = Event ( ** data ) <EOL> recurring_event . save ( ) <EOL> self . period = Period ( events = Event . objects . all ( ) , <EOL> start = datetime . datetime ( datetime . datetime . now ( ) . year , <NUM_LIT:1> , <NUM_LIT:4> , <NUM_LIT:7> , <NUM_LIT:0> , tzinfo = pytz . utc ) , <EOL> end = datetime . datetime ( datetime . datetime . now ( ) . year , <NUM_LIT:1> , <NUM_LIT> , <NUM_LIT:7> , <NUM_LIT:0> , tzinfo = pytz . utc ) ) <EOL> def test_querystring_for_datetime ( self ) : <EOL> date = datetime . datetime ( datetime . datetime . now ( ) . year , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> query_string = querystring_for_date ( date ) <EOL> self . assertEqual ( escape ( '<STR_LIT>' . format ( datetime . datetime . now ( ) . year ) ) , <EOL> query_string ) <EOL> def test_prev_url ( self ) : <EOL> query_string = prev_url ( '<STR_LIT>' , self . cal , self . day ) <EOL> url_params = escape ( '<STR_LIT>' . format ( datetime . datetime . now ( ) . year ) ) <EOL> expected = ( '<STR_LIT>' . format ( url_params ) ) <EOL> self . assertEqual ( query_string , expected ) <EOL> def test_next_url ( self ) : <EOL> query_string = next_url ( '<STR_LIT>' , self . cal , self . day ) <EOL> url_params = escape ( '<STR_LIT>' . format ( datetime . datetime . now ( ) . year ) ) <EOL> expected = ( '<STR_LIT>' . format ( url_params ) ) <EOL> self . assertEqual ( query_string , expected ) <EOL> def test_next_url_upper_limit ( self ) : <EOL> query_string = next_url ( '<STR_LIT>' , self . cal , self . day_out_of_limit ) <EOL> self . assertEqual ( query_string , '<STR_LIT>' ) <EOL> def test_prev_url_lower_limit ( self ) : <EOL> query_string = prev_url ( '<STR_LIT>' , self . cal , self . day_out_of_limit_lower ) <EOL> self . assertEqual ( query_string , '<STR_LIT>' ) <EOL> def test_create_event_url ( self ) : <EOL> context = { } <EOL> slot = self . period . get_time_slot ( datetime . datetime ( datetime . datetime . now ( ) . year , <NUM_LIT:1> , <NUM_LIT:4> , <NUM_LIT:7> , <NUM_LIT:0> , tzinfo = pytz . utc ) , <EOL> datetime . datetime ( datetime . datetime . now ( ) . year , <NUM_LIT:1> , <NUM_LIT:4> , <NUM_LIT:7> , <NUM_LIT:12> , tzinfo = pytz . utc ) ) <EOL> query_string = create_event_url ( context , self . cal , slot . start ) <EOL> expected = ( '<STR_LIT>' . format ( datetime . datetime . now ( ) . year ) ) <EOL> self . assertEqual ( query_string [ '<STR_LIT>' ] , escape ( expected ) ) <EOL> def test_all_day_event_cook_slots ( self ) : <EOL> cal = Calendar ( name = '<STR_LIT>' , slug = '<STR_LIT>' ) <EOL> cal . save ( ) <EOL> start = datetime . datetime ( <EOL> datetime . datetime . now ( ) . year , <NUM_LIT:1> , <NUM_LIT:5> , <NUM_LIT:0> , <NUM_LIT:0> , tzinfo = pytz . utc ) <EOL> end = datetime . datetime ( <EOL> datetime . datetime . now ( ) . year , <NUM_LIT:1> , <NUM_LIT:6> , <NUM_LIT:0> , <NUM_LIT:0> , tzinfo = pytz . utc ) <EOL> data = { <EOL> '<STR_LIT:title>' : '<STR_LIT>' , <EOL> '<STR_LIT:start>' : start , <EOL> '<STR_LIT:end>' : end , <EOL> '<STR_LIT>' : self . cal , <EOL> } <EOL> event = Event ( ** data ) <EOL> event . save ( ) <EOL> period = Day ( [ event ] , start , end ) <EOL> slots = _cook_slots ( period , <NUM_LIT> ) <EOL> self . assertEqual ( len ( slots ) , <NUM_LIT> ) </s>
<s> """<STR_LIT>""" <EOL> import os <EOL> DO_LOG = os . environ . get ( "<STR_LIT>" , False ) <EOL> def on_message ( msg , server ) : <EOL> if DO_LOG : <EOL> server . query ( "<STR_LIT>" , <EOL> msg [ "<STR_LIT:text>" ] , msg [ "<STR_LIT:user>" ] , msg [ "<STR_LIT>" ] , msg [ "<STR_LIT>" ] , msg [ "<STR_LIT>" ] ) <EOL> def on_init ( server ) : <EOL> if DO_LOG : <EOL> server . query ( """<STR_LIT>""" ) </s>
<s> import os <EOL> import sys <EOL> from nose . tools import eq_ <EOL> import vcr <EOL> DIR = os . path . dirname ( os . path . realpath ( __file__ ) ) <EOL> sys . path . insert ( <NUM_LIT:0> , os . path . join ( DIR , '<STR_LIT>' ) ) <EOL> from mlb import on_message <EOL> def test_basic ( ) : <EOL> with vcr . use_cassette ( '<STR_LIT>' ) : <EOL> ret = on_message ( { "<STR_LIT:text>" : u"<STR_LIT>" } , None ) <EOL> assert "<STR_LIT>" in ret </s>
<s> from gen import Tree <EOL> from demo_trees import trees <EOL> from reingold_thread import reingold_tilford as rtc <EOL> from reingold_thread import p as printtree <EOL> import unittest <EOL> class TreeTest ( unittest . TestCase ) : <EOL> def assertTree ( self , tree , expected ) : <EOL> self . assertEqual ( tree . x , expected [ <NUM_LIT:0> ] ) <EOL> for i in range ( <NUM_LIT:1> , len ( tree . children ) + <NUM_LIT:1> ) : <EOL> child = tree . children [ i - <NUM_LIT:1> ] <EOL> exp = expected [ i ] <EOL> if isinstance ( exp , int ) : self . assertTree ( child , [ exp ] ) <EOL> else : self . assertTree ( child , exp ) <EOL> class TestBinTree ( TreeTest ) : <EOL> def testSimple ( self ) : <EOL> dt = self . f ( trees [ <NUM_LIT:0> ] ) <EOL> self . assertTree ( dt , [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:2> ] ) <EOL> def testDeepLeft ( self ) : <EOL> dt = self . f ( trees [ <NUM_LIT:1> ] ) <EOL> expected = [ <NUM_LIT:1> , [ <NUM_LIT:0> , [ <NUM_LIT:0> , [ <NUM_LIT:0> , [ <NUM_LIT:0> ] ] ] ] , <NUM_LIT:2> ] <EOL> self . assertTree ( dt , expected ) <EOL> def testDeepRight ( self ) : <EOL> dt = self . f ( trees [ <NUM_LIT:2> ] ) <EOL> expected = [ <NUM_LIT:1> , <NUM_LIT:0> , [ <NUM_LIT:2> , [ <NUM_LIT:2> , [ <NUM_LIT:2> , <NUM_LIT:2> ] ] ] ] <EOL> self . assertTree ( dt , expected ) <EOL> def testTightright ( self ) : <EOL> dt = self . f ( trees [ <NUM_LIT:3> ] ) <EOL> expected = [ <NUM_LIT:2> , [ <NUM_LIT:1> , [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:2> ] ] , [ <NUM_LIT:3> , <NUM_LIT:2> , [ <NUM_LIT:4> , <NUM_LIT:3> , <NUM_LIT:5> ] ] ] <EOL> self . assertTree ( dt , expected ) <EOL> def testUnbalanced ( self ) : <EOL> dt = self . f ( trees [ <NUM_LIT:4> ] ) <EOL> expected = [ <NUM_LIT:6> , [ <NUM_LIT:4> , [ <NUM_LIT:3> , [ <NUM_LIT:2> , [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:2> ] , <NUM_LIT:3> ] , <NUM_LIT:4> ] , <NUM_LIT:5> ] , [ <NUM_LIT:8> , [ <NUM_LIT:7> , <NUM_LIT:6> , <NUM_LIT:8> ] , <NUM_LIT:9> ] ] <EOL> self . assertTree ( dt , expected ) <EOL> def testWSTree ( self ) : <EOL> dt = self . f ( trees [ <NUM_LIT:5> ] ) <EOL> expected = [ <NUM_LIT:3> , [ <NUM_LIT:1> , <NUM_LIT:0> , [ <NUM_LIT:2> , <NUM_LIT:1> , <NUM_LIT:3> ] ] , [ <NUM_LIT:5> , [ <NUM_LIT:5> , [ <NUM_LIT:5> , [ <NUM_LIT:5> , [ <NUM_LIT:4> , <NUM_LIT:3> , <NUM_LIT:5> ] , <NUM_LIT:6> ] ] ] ] ] <EOL> self . assertTree ( dt , expected ) <EOL> def testBuchMerge ( self ) : <EOL> dt = self . f ( trees [ <NUM_LIT:6> ] ) <EOL> expected = [ <NUM_LIT:3> , [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:2> ] , [ <NUM_LIT:5> , <NUM_LIT:4> , <NUM_LIT:6> ] ] <EOL> self . assertTree ( dt , expected ) <EOL> class TestNaryTree ( TreeTest ) : <EOL> def testBuchheim ( self ) : <EOL> dt = self . f ( trees [ <NUM_LIT:7> ] ) <EOL> expected = [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ] <EOL> self . assertTree ( dt , expected ) <EOL> class TestNaive ( TestBinTree ) : <EOL> def setUp ( self ) : <EOL> import reingold_naive <EOL> self . f = reingold_naive . reingold_tilford <EOL> class TestThread ( TestBinTree ) : <EOL> def setUp ( self ) : <EOL> import reingold_thread <EOL> self . f = reingold_thread . reingold_tilford <EOL> class TestAddMod ( TestBinTree ) : <EOL> def setUp ( self ) : <EOL> import reingold_addmod <EOL> self . f = reingold_addmod . layout <EOL> class TestBuchheim ( TestBinTree , TestNaryTree ) : <EOL> def setUp ( self ) : <EOL> import buchheim <EOL> self . f = buchheim . buchheim <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> load = unittest . TestLoader ( ) . loadTestsFromTestCase <EOL> suite = load ( TestNaive ) <EOL> suite . addTests ( load ( TestThread ) ) <EOL> suite . addTests ( load ( TestAddMod ) ) <EOL> unittest . TextTestRunner ( verbosity = <NUM_LIT:2> ) . run ( suite ) </s>
<s> import base64 <EOL> from laikaboss . objectmodel import ModuleObject , ExternalVars <EOL> from laikaboss . si_module import SI_MODULE <EOL> class DECODE_BASE64 ( SI_MODULE ) : <EOL> def __init__ ( self , ) : <EOL> self . module_name = "<STR_LIT>" <EOL> def _run ( self , scanObject , result , depth , args ) : <EOL> moduleResult = [ ] <EOL> try : <EOL> decoded = base64 . b64decode ( scanObject . buffer ) <EOL> moduleResult . append ( ModuleObject ( buffer = decoded , externalVars = ExternalVars ( filename = "<STR_LIT>" % len ( decoded ) ) ) ) <EOL> return moduleResult <EOL> except : <EOL> raise </s>
<s> '''<STR_LIT>''' <EOL> import sys <EOL> import traceback <EOL> import os <EOL> import StringIO <EOL> import re <EOL> import json <EOL> import Milter <EOL> import resource <EOL> import time <EOL> from time import strftime <EOL> import socket <EOL> import syslog <EOL> import zmq <EOL> import datetime <EOL> import random <EOL> import ConfigParser <EOL> from subprocess import Popen , PIPE , STDOUT <EOL> import zlib <EOL> import uuid <EOL> import cPickle as pickle <EOL> from email . utils import formatdate <EOL> from email . utils import parsedate_tz <EOL> from email . utils import mktime_tz <EOL> from laikaboss . objectmodel import ExternalObject , ExternalVars <EOL> from laikaboss . constants import level_minimal , level_metadata , level_full <EOL> from laikaboss . clientLib import Client , flagRollup , getAttachmentList , dispositionFromResult , finalDispositionFromResult <EOL> global_whitelist = { } <EOL> class LaikaMilter ( Milter . Base ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . altConfig = None <EOL> if len ( sys . argv ) == <NUM_LIT:2> : <EOL> self . altConfig = sys . argv [ <NUM_LIT:1> ] <EOL> self . fph = None <EOL> self . fpb = None <EOL> self . headers = [ ] <EOL> self . fileBuffer = "<STR_LIT>" <EOL> self . rulesMatched = "<STR_LIT>" <EOL> self . warnings = "<STR_LIT>" <EOL> self . sender = "<STR_LIT>" <EOL> self . receiver = "<STR_LIT>" <EOL> self . disposition = "<STR_LIT>" <EOL> self . dispositions = "<STR_LIT>" <EOL> self . scanPerformedOn = "<STR_LIT>" <EOL> self . subject = "<STR_LIT>" <EOL> self . qid = "<STR_LIT>" <EOL> self . messageID = "<STR_LIT>" <EOL> self . messageDate = "<STR_LIT>" <EOL> self . attachments = "<STR_LIT>" <EOL> self . CUSTOMHELO = "<STR_LIT>" <EOL> self . CUSTOMFROM = "<STR_LIT>" <EOL> self . CUSTOMORCPT = [ ] <EOL> self . milterConfig = MilterConfig ( ) <EOL> if self . altConfig : <EOL> self . milterConfig . configFileLoc = self . altConfig <EOL> self . milterConfig . loadAll ( ) <EOL> self . archiveFileName = milterConfig . storeDir <EOL> self . logger = logger <EOL> self . startTimeDT = None <EOL> self . startTime = None <EOL> self . endTime = None <EOL> self . startTimeZMQ = None <EOL> self . endTimeZMQ = None <EOL> self . rtnToMTA = Milter . CONTINUE <EOL> self . alreadyScanned = False <EOL> self . uuid = str ( uuid . uuid4 ( ) ) [ - <NUM_LIT:12> : ] <EOL> def abort ( self ) : <EOL> try : <EOL> log = self . uuid + "<STR_LIT>" + self . qid + "<STR_LIT>" <EOL> self . logger . writeLog ( syslog . LOG_DEBUG , "<STR_LIT:%s>" % ( str ( log ) ) ) <EOL> self . eom ( ) <EOL> except : <EOL> log = self . uuid + "<STR_LIT>" <EOL> self . logger . writeLog ( syslog . LOG_ERR , "<STR_LIT:%s>" % ( str ( log ) ) ) <EOL> return Milter . CONTINUE <EOL> def unknown ( self , cmd ) : <EOL> log = self . uuid + "<STR_LIT>" + str ( cmd ) <EOL> self . logger . writeLog ( syslog . LOG_DEBUG , "<STR_LIT:%s>" % ( str ( log ) ) ) <EOL> return Milter . CONTINUE <EOL> def hello ( self , heloname ) : <EOL> returnval = Milter . CONTINUE <EOL> try : <EOL> self . logger . writeLog ( syslog . LOG_DEBUG , "<STR_LIT>" % ( self . uuid , str ( heloname ) , str ( self . _getClientAddr ( ) ) , str ( self . _getIfAddr ( ) ) ) ) <EOL> self . CUSTOMHELO = heloname <EOL> except : <EOL> log = self . uuid + "<STR_LIT>" <EOL> self . logger . writeLog ( syslog . LOG_ERR , "<STR_LIT:%s>" % ( str ( log ) ) ) <EOL> return returnval <EOL> def envfrom ( self , f , * str ) : <EOL> try : <EOL> if self . CUSTOMFROM != "<STR_LIT>" : <EOL> self . __init__ ( ) <EOL> self . startTime = time . time ( ) <EOL> self . startTimeDT = datetime . datetime . now ( ) <EOL> if ( self . milterConfig . mode == "<STR_LIT>" ) : <EOL> log = self . uuid + "<STR_LIT>" + '<STR_LIT>' . join ( f ) + "<STR_LIT:]>" <EOL> self . logger . writeLog ( syslog . LOG_DEBUG , "<STR_LIT:%s>" % ( log ) ) <EOL> return self . milterConfig . dispositionModes [ "<STR_LIT>" . lower ( ) ] <EOL> log = self . uuid + "<STR_LIT>" <EOL> log += '<STR_LIT>' . join ( f ) <EOL> self . logger . writeLog ( syslog . LOG_DEBUG , "<STR_LIT:%s>" % ( log ) ) <EOL> self . CUSTOMFROM = f <EOL> self . CUSTOMFROM = self . CUSTOMFROM . replace ( "<STR_LIT:<>" , "<STR_LIT>" ) <EOL> self . CUSTOMFROM = self . CUSTOMFROM . replace ( "<STR_LIT:>>" , "<STR_LIT>" ) <EOL> self . sender = self . CUSTOMFROM <EOL> self . fph = StringIO . StringIO ( ) <EOL> self . fpb = StringIO . StringIO ( ) <EOL> except : <EOL> log = self . uuid + "<STR_LIT>" <EOL> self . logger . writeLog ( syslog . LOG_ERR , "<STR_LIT:%s>" % ( str ( log ) ) ) <EOL> return Milter . CONTINUE <EOL> def envrcpt ( self , to , * str ) : <EOL> try : <EOL> self . CUSTOMORCPT . append ( to ) <EOL> log = self . uuid + "<STR_LIT>" <EOL> rcpt = '<STR_LIT:U+0020>' . join ( self . CUSTOMORCPT ) <EOL> log += rcpt <EOL> self . logger . writeLog ( syslog . LOG_DEBUG , "<STR_LIT:%s>" % ( log ) ) <EOL> self . receiver = rcpt <EOL> self . receiver = self . receiver . replace ( "<STR_LIT:<>" , "<STR_LIT>" ) <EOL> self . receiver = self . receiver . replace ( "<STR_LIT:>>" , "<STR_LIT>" ) <EOL> except : <EOL> log = self . uuid + "<STR_LIT>" <EOL> self . logger . writeLog ( syslog . LOG_ERR , "<STR_LIT:%s>" % ( str ( log ) ) ) <EOL> return Milter . CONTINUE <EOL> def header ( self , name , val ) : <EOL> try : <EOL> lname = name . lower ( ) <EOL> tname = name . strip ( ) <EOL> tval = val . strip ( ) <EOL> headerstring = "<STR_LIT>" % ( name , <EOL> val <EOL> ) <EOL> self . headers . append ( headerstring ) <EOL> self . _getSpecialHeaderLines ( lname , val ) <EOL> except : <EOL> log = self . uuid + "<STR_LIT>" <EOL> self . logger . writeLog ( syslog . LOG_ERR , "<STR_LIT:%s>" % ( str ( log ) ) ) <EOL> return Milter . CONTINUE <EOL> def eoh ( self ) : <EOL> try : <EOL> self . _writeHeaderToFP ( ) <EOL> except : <EOL> log = self . uuid + "<STR_LIT>" <EOL> self . logger . writeLog ( syslog . LOG_ERR , "<STR_LIT:%s>" % ( str ( log ) ) ) <EOL> return Milter . CONTINUE <EOL> def body ( self , chunk ) : <EOL> try : <EOL> if ( isinstance ( chunk , str ) ) : <EOL> chunk = chunk . replace ( "<STR_LIT:\r\n>" , "<STR_LIT:\n>" ) <EOL> self . fpb . write ( chunk ) <EOL> self . fpb . flush ( ) <EOL> except : <EOL> log = self . uuid + "<STR_LIT>" <EOL> self . logger . writeLog ( syslog . LOG_ERR , "<STR_LIT:%s>" % ( str ( log ) ) ) <EOL> return Milter . CONTINUE <EOL> def eom ( self ) : <EOL> try : <EOL> self . _getQID ( ) <EOL> checklist = "<STR_LIT>" % ( str ( self . _getIfAddr ( ) ) , str ( self . _getClientAddr ( ) ) ) <EOL> if checklist in global_whitelist : <EOL> self . logger . writeLog ( syslog . LOG_DEBUG , "<STR_LIT>" % ( self . uuid , str ( self . qid ) , str ( self . _getClientAddr ( ) ) , str ( self . _getIfAddr ( ) ) ) ) <EOL> self . rtnToMTA = Milter . ACCEPT <EOL> else : <EOL> if not self . alreadyScanned : <EOL> self . _getBufferFromFP ( ) <EOL> self . _generateArchiveFileName ( ) <EOL> self . rtnToMTA = self . _dispositionMessage ( ) <EOL> self . fph . write ( "<STR_LIT>" % ( self . milterConfig . MailscanResultHeaderString , self . disposition ) ) <EOL> self . fph . write ( "<STR_LIT>" % ( "<STR_LIT>" , self . rulesMatched ) ) <EOL> self . _getBufferFromFP ( ) <EOL> self . fph . close ( ) <EOL> self . fpb . close ( ) <EOL> self . _writeFileToDisk ( ) <EOL> self . _addHeaders ( ) <EOL> self . _logDetails ( ) <EOL> self . _logEachEnvelope ( ) <EOL> log = self . uuid + "<STR_LIT:U+0020>" + self . qid + "<STR_LIT>" + self . milterConfig . _unconvertDispositionMode ( self . rtnToMTA ) <EOL> self . logger . writeLog ( syslog . LOG_DEBUG , "<STR_LIT:%s>" % ( str ( log ) ) ) <EOL> self . alreadyScanned = True <EOL> else : <EOL> log = self . uuid + "<STR_LIT>" + self . archiveFileName + "<STR_LIT>" <EOL> self . logger . writeLog ( syslog . LOG_DEBUG , "<STR_LIT:%s>" % ( str ( log ) ) ) <EOL> except Exception as e : <EOL> exc_type , exc_value , exc_traceback = sys . exc_info ( ) <EOL> ERROR_INFO = repr ( traceback . format_exception ( exc_type , exc_value , exc_traceback ) ) <EOL> ERROR = "<STR_LIT>" % ( self . uuid , self . rtnToMTA , ERROR_INFO ) <EOL> print ERROR <EOL> self . logger . writeLog ( syslog . LOG_ERR , "<STR_LIT:%s>" % ( str ( ERROR ) ) ) <EOL> return self . rtnToMTA <EOL> def _addHeader ( self , name , value ) : <EOL> try : <EOL> if value : <EOL> self . addheader ( name , value ) <EOL> except Exception as e : <EOL> exc_type , exc_value , exc_traceback = sys . exc_info ( ) <EOL> log = "<STR_LIT>" % ( self . uuid , repr ( traceback . format_exception ( exc_type , exc_value , exc_traceback ) ) ) <EOL> self . logger . writeLog ( syslog . LOG_ERR , "<STR_LIT:%s>" % ( str ( log ) ) ) <EOL> log = "<STR_LIT>" % ( name , value ) <EOL> self . logger . writeLog ( syslog . LOG_ERR , "<STR_LIT>" % ( self . uuid , str ( log ) ) ) <EOL> def _addHeaders ( self ) : <EOL> if ( self . milterConfig . ApplyMailscanResultHeader ) : <EOL> self . _addHeader ( self . milterConfig . MailscanResultHeaderString , self . disposition ) <EOL> if ( self . milterConfig . ApplyCustomHeaders ) : <EOL> self . _addHeader ( "<STR_LIT>" % ( self . milterConfig . CustomHeaderBase ) , self . CUSTOMHELO ) <EOL> self . _addHeader ( "<STR_LIT>" % ( self . milterConfig . CustomHeaderBase ) , self . sender ) <EOL> self . _addHeader ( "<STR_LIT>" % ( self . milterConfig . CustomHeaderBase ) , self . receiver ) <EOL> def _getMboxLine ( self ) : <EOL> cleanSender = self . sender . replace ( "<STR_LIT:<>" , "<STR_LIT>" ) <EOL> cleanSender = cleanSender . replace ( "<STR_LIT:>>" , "<STR_LIT>" ) <EOL> POSIXtime = time . asctime ( self . startTimeDT . timetuple ( ) ) <EOL> envelope_from_extra = "<STR_LIT>" <EOL> mbox_from_line = "<STR_LIT>" + cleanSender + "<STR_LIT:U+0020>" + str ( POSIXtime ) + "<STR_LIT:U+0020>" + envelope_from_extra <EOL> return mbox_from_line <EOL> def _checkFilePath ( self , path ) : <EOL> try : <EOL> if not os . path . exists ( path ) : <EOL> os . makedirs ( path ) <EOL> except OSError : <EOL> log = "<STR_LIT>" + path <EOL> self . logger . writeLog ( syslog . LOG_ERR , "<STR_LIT:%s>" % ( str ( log ) ) ) <EOL> def _checkOKToContinueWithOpenFiles ( self ) : <EOL> okToContinue = True <EOL> try : <EOL> pid = os . getpid ( ) <EOL> try : <EOL> fd_dir = os . path . join ( '<STR_LIT>' , str ( pid ) , '<STR_LIT>' ) <EOL> except : <EOL> self . logger . writeLog ( syslog . LOG_DEBUG , "<STR_LIT>" + str ( pid ) ) <EOL> numOpenFilesOutput = <NUM_LIT:0> <EOL> for file in os . listdir ( fd_dir ) : <EOL> numOpenFilesOutput += <NUM_LIT:1> <EOL> if ( int ( numOpenFilesOutput ) > int ( self . milterConfig . maxFiles ) ) : <EOL> self . logger . writeLog ( syslog . LOG_ERR , "<STR_LIT>" + str ( numOpenFilesOutput ) + "<STR_LIT>" + str ( self . milterConfig . dispositionModes [ "<STR_LIT>" . lower ( ) ] ) + "<STR_LIT>" + str ( self . milterConfig . maxFiles ) ) <EOL> okToContinue = False <EOL> else : <EOL> self . logger . writeLog ( syslog . LOG_DEBUG , self . milterConfig . milterInstance + "<STR_LIT>" + str ( numOpenFilesOutput ) + "<STR_LIT>" + str ( self . milterConfig . maxFiles ) ) <EOL> except ValueError : <EOL> self . logger . writeLog ( syslog . LOG_ERR , "<STR_LIT>" ) <EOL> except Exception as e : <EOL> exc_type , exc_value , exc_traceback = sys . exc_info ( ) <EOL> print "<STR_LIT>" % ( repr ( traceback . format_exception ( exc_type , exc_value , exc_traceback ) ) ) <EOL> self . logger . writeLog ( syslog . LOG_ERR , "<STR_LIT>" ) <EOL> return okToContinue <EOL> def _dispositionMessage ( self ) : <EOL> self . startTimeZMQ = time . time ( ) <EOL> if ( not self . _checkOKToContinueWithOpenFiles ( ) ) : <EOL> self . disposition = "<STR_LIT>" <EOL> self . logger . writeLog ( syslog . LOG_ERR , "<STR_LIT>" % ( str ( self . disposition ) ) ) <EOL> rtnToMTA = self . milterConfig . dispositionModes [ "<STR_LIT>" . lower ( ) ] <EOL> else : <EOL> dispositioner = Dispositioner ( self . logger ) <EOL> success = dispositioner . zmqGetFlagswithRetry ( self . milterConfig . zmqMaxRetry , self ) <EOL> self . disposition = dispositioner . strScanResult <EOL> self . scanPerformedOn = dispositioner . scanServerToUse <EOL> self . rulesMatched = '<STR_LIT:U+0020>' . join ( dispositioner . match ) <EOL> self . warnings = dispositioner . warnings <EOL> self . attachments = dispositioner . attachements <EOL> self . _dispositionMessage_AttachmentHelper ( ) <EOL> self . dispositions = dispositioner . dispositions <EOL> self . logger . writeLog ( syslog . LOG_DEBUG , "<STR_LIT>" % ( self . uuid , str ( dispositioner . dispositions ) ) ) <EOL> rtnToMTA = self . _getReturnToMTAValue ( dispositioner ) <EOL> dispositioner . close ( ) <EOL> self . endTimeZMQ = time . time ( ) <EOL> return rtnToMTA <EOL> def _dispositionMessage_AttachmentHelper ( self ) : <EOL> if ( len ( self . attachments ) > <NUM_LIT> ) : <EOL> commaLoc = self . attachments . find ( "<STR_LIT:U+002C>" , <NUM_LIT> , <NUM_LIT> ) <EOL> attachmentsReduced = self . attachments [ : commaLoc ] <EOL> self . attachments = attachmentsReduced <EOL> self . attachments += "<STR_LIT>" <EOL> return True <EOL> def _generateArchiveFileName ( self ) : <EOL> if self . milterConfig . storeEmails : <EOL> self . archiveFileName = self . milterConfig . storeDir <EOL> now = datetime . datetime . now ( ) <EOL> unixNow = time . time ( ) <EOL> strCustomDateFolderFormat = now . strftime ( self . milterConfig . customFolderDateFormat ) <EOL> randNum = str ( random . random ( ) ) <EOL> uniqueNum = self . qid <EOL> if not uniqueNum : <EOL> uniqueNum = randNum <EOL> ifAddr = self . _getIfAddr ( ) <EOL> clientAddr = self . _getClientAddr ( ) <EOL> self . archiveFileName += strCustomDateFolderFormat <EOL> self . _checkFilePath ( self . archiveFileName ) <EOL> self . archiveFileName += "<STR_LIT>" + str ( int ( unixNow ) ) + "<STR_LIT:.>" + uniqueNum + "<STR_LIT:.>" + str ( clientAddr ) + "<STR_LIT:.>" + str ( ifAddr ) <EOL> else : <EOL> self . archiveFileName = "<STR_LIT>" <EOL> def _getBufferFromFP ( self ) : <EOL> self . fph . flush ( ) <EOL> self . fpb . flush ( ) <EOL> self . fileBuffer = self . fph . getvalue ( ) <EOL> self . fileBuffer += self . fpb . getvalue ( ) <EOL> def _getClientAddr ( self ) : <EOL> Addr = self . getsymval ( "<STR_LIT>" ) <EOL> return Addr <EOL> def _getIfAddr ( self ) : <EOL> Addr = self . getsymval ( "<STR_LIT>" ) <EOL> return Addr <EOL> def _getQID ( self ) : <EOL> self . qid = self . getsymval ( "<STR_LIT>" ) <EOL> def _getReturnToMTAValue ( self , dispositioner ) : <EOL> if ( dispositioner . strScanResult . lower ( ) in self . milterConfig . dispositionModes ) : <EOL> rtnToMTA = self . milterConfig . dispositionModes [ dispositioner . strScanResult . lower ( ) ] <EOL> else : <EOL> rtnToMTA = self . milterConfig . dispositionModes [ "<STR_LIT:default>" ] <EOL> self . logger . writeLog ( syslog . LOG_ERR , "<STR_LIT>" % ( str ( dispositioner . strScanResult . lower ( ) ) , str ( self . milterConfig . dispositionModes [ "<STR_LIT:default>" ] ) ) ) <EOL> return rtnToMTA <EOL> def _getSpecialHeaderLines ( self , lname , val ) : <EOL> if ( lname == "<STR_LIT>" ) : <EOL> self . subject = val <EOL> if ( lname == "<STR_LIT>" ) : <EOL> self . messageID = val <EOL> self . messageID = self . messageID . replace ( "<STR_LIT:<>" , "<STR_LIT>" ) <EOL> self . messageID = self . messageID . replace ( "<STR_LIT:>>" , "<STR_LIT>" ) <EOL> if ( lname == "<STR_LIT:date>" ) : <EOL> try : <EOL> self . messageDate = formatdate ( mktime_tz ( parsedate_tz ( val . split ( '<STR_LIT:\n>' ) [ <NUM_LIT:0> ] ) ) , True ) <EOL> except : <EOL> log = self . uuid + "<STR_LIT>" + str ( val ) + "<STR_LIT>" <EOL> self . logger . writeLog ( syslog . LOG_ERR , "<STR_LIT:%s>" % ( str ( log ) ) ) <EOL> def _logDetails ( self ) : <EOL> log = self . uuid + "<STR_LIT>" % self . rulesMatched <EOL> self . logger . writeLog ( syslog . LOG_DEBUG , "<STR_LIT:%s>" % ( str ( log ) ) ) <EOL> def _logEachEnvelope ( self ) : <EOL> while ( len ( self . CUSTOMORCPT ) > <NUM_LIT:0> ) : <EOL> individualRCPT = self . CUSTOMORCPT . pop ( ) <EOL> individualRCPT = individualRCPT . replace ( "<STR_LIT:<>" , "<STR_LIT>" ) <EOL> individualRCPT = individualRCPT . replace ( "<STR_LIT:>>" , "<STR_LIT>" ) <EOL> self . _logMail ( individualRCPT ) <EOL> def _logMail ( self , individualReceiver ) : <EOL> if self . rulesMatched == "<STR_LIT:None>" : <EOL> self . rulesMatched = "<STR_LIT>" <EOL> self . endTime = time . time ( ) <EOL> timeDiff = int ( ( self . endTime - self . startTime ) * <NUM_LIT> ) <EOL> timeDiffZMQ = int ( ( self . endTimeZMQ - self . startTimeZMQ ) * <NUM_LIT> ) <EOL> log = "<STR_LIT>" % ( self . _logMail_sanitize ( self . archiveFileName ) , <EOL> self . _logMail_sanitize ( self . messageID ) , <EOL> self . _logMail_sanitize ( self . milterConfig . milterInstance ) , <EOL> self . _logMail_sanitize ( self . rulesMatched ) , <EOL> self . _logMail_sanitize ( self . dispositions ) , <EOL> self . _logMail_sanitize ( self . attachments ) , <EOL> self . _logMail_sanitize ( self . sender ) , <EOL> self . _logMail_sanitize ( self . _getClientAddr ( ) ) , <EOL> self . _logMail_sanitize ( individualReceiver ) , <EOL> self . _logMail_sanitize ( self . _getIfAddr ( ) ) , <EOL> self . _logMail_sanitize ( self . messageDate ) , <EOL> self . _logMail_sanitize ( self . milterConfig . _unconvertDispositionMode ( self . rtnToMTA ) ) , <EOL> self . _logMail_sanitize ( self . qid ) , <EOL> self . _logMail_sanitize ( "<STR_LIT>" ) , <EOL> self . _logMail_sanitize ( timeDiffZMQ ) , <EOL> self . _logMail_sanitize ( self . subject ) ) <EOL> self . logger . writeLog ( syslog . LOG_ALERT , "<STR_LIT:%s>" % ( str ( log ) ) ) <EOL> def _logMail_sanitize ( self , inputString ) : <EOL> strInputString = str ( inputString ) <EOL> log_delimiter = "<STR_LIT:|>" <EOL> log_delimiter_replacement = "<STR_LIT:_>" <EOL> return strInputString . replace ( log_delimiter , log_delimiter_replacement ) <EOL> def _writeFileToDisk ( self ) : <EOL> if self . archiveFileName : <EOL> try : <EOL> fp = open ( self . archiveFileName , "<STR_LIT:wb>" ) <EOL> fp . write ( self . fileBuffer ) <EOL> fp . flush ( ) <EOL> fp . close ( ) <EOL> except IOError : <EOL> log = self . uuid + "<STR_LIT>" + self . archiveFileName + "<STR_LIT>" <EOL> self . logger . writeLog ( syslog . LOG_ERR , "<STR_LIT:%s>" % ( str ( log ) ) ) <EOL> def _writeHeaderToFP ( self ) : <EOL> self . fph . write ( self . _getMboxLine ( ) + "<STR_LIT:\n>" ) <EOL> for header in self . headers : <EOL> self . fph . write ( header ) <EOL> clientAddr = self . _getClientAddr ( ) <EOL> self . fph . write ( "<STR_LIT>" % ( self . milterConfig . CustomHeaderBase , self . CUSTOMHELO , clientAddr ) ) <EOL> self . fph . write ( "<STR_LIT>" % ( self . milterConfig . CustomHeaderBase , self . sender ) ) <EOL> self . fph . write ( "<STR_LIT>" % ( self . milterConfig . CustomHeaderBase , self . receiver ) ) <EOL> self . fpb . write ( "<STR_LIT:\n>" ) <EOL> class Dispositioner ( ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , logger ) : <EOL> self . altConfig = None <EOL> self . client = None <EOL> if len ( sys . argv ) == <NUM_LIT:2> : <EOL> self . altConfig = sys . argv [ <NUM_LIT:1> ] <EOL> self . match = [ ] <EOL> self . strScanResult = "<STR_LIT>" <EOL> self . dispositions = "<STR_LIT>" <EOL> self . warnings = "<STR_LIT>" <EOL> self . scanServerToUse = "<STR_LIT:None>" <EOL> self . numScanSigsMatched = - <NUM_LIT:1> <EOL> self . currentScanServerNum = - <NUM_LIT:1> <EOL> self . milterConfig = MilterConfig ( ) <EOL> if self . altConfig : <EOL> self . milterConfig . configFileLoc = self . altConfig <EOL> self . milterConfig . loadAll ( ) <EOL> self . logger = logger <EOL> self . attachements = "<STR_LIT>" <EOL> def close ( self ) : <EOL> try : <EOL> self . client . close ( ) <EOL> except : <EOL> self . logger . writeLog ( syslog . LOG_ERR , self . uuid + "<STR_LIT>" ) <EOL> def zmqGetFlagswithRetry ( self , numRetries , milterContext ) : <EOL> sendResponse = self . _zmqGetFlags ( numRetries , milterContext ) <EOL> def _getNextScanServer ( self ) : <EOL> if ( len ( self . milterConfig . servers ) > <NUM_LIT:1> ) : <EOL> self . currentScanServerNum = ( self . currentScanServerNum + <NUM_LIT:1> ) % ( len ( self . milterConfig . servers ) - <NUM_LIT:1> ) <EOL> self . scanServerToUse = self . milterConfig . servers [ self . currentScanServerNum ] <EOL> else : <EOL> self . scanServerToUse = self . milterConfig . servers [ <NUM_LIT:0> ] <EOL> return self . scanServerToUse <EOL> def _getRandomScanServer ( self ) : <EOL> randServer = <NUM_LIT:0> <EOL> server = "<STR_LIT>" <EOL> numServers = len ( self . milterConfig . servers ) <EOL> if ( numServers > <NUM_LIT:1> ) : <EOL> randServer = random . randint ( <NUM_LIT:0> , numServers - <NUM_LIT:1> ) <EOL> self . scanServerToUse = self . milterConfig . servers [ randServer ] <EOL> return self . scanServerToUse <EOL> def _zmqGetFlags ( self , numRetries , milterContext ) : <EOL> REQUEST_TIMEOUT = milterContext . milterConfig . zmqTimeout <EOL> gotResponseFromScanner = - <NUM_LIT:1> <EOL> if ( len ( self . milterConfig . servers ) > <NUM_LIT:0> ) : <EOL> SERVER_ENDPOINT = self . _getNextScanServer ( ) <EOL> gotResponseFromScanner = - <NUM_LIT:1> <EOL> gotResponseFromScanner = self . _zmqSendBuffer ( milterContext , numRetries , REQUEST_TIMEOUT , SERVER_ENDPOINT ) <EOL> else : <EOL> log = "<STR_LIT>" <EOL> self . logger . writeLog ( syslog . LOG_DEBUG , "<STR_LIT:%s>" % ( str ( log ) ) ) <EOL> return gotResponseFromScanner <EOL> def _zmqSendBuffer ( self , milterContext , numRetries , REQUEST_TIMEOUT , SERVER_ENDPOINT ) : <EOL> gotResponseFromScanner = - <NUM_LIT:1> <EOL> self . client = Client ( SERVER_ENDPOINT ) <EOL> log = milterContext . uuid + "<STR_LIT>" + str ( milterContext . qid ) + "<STR_LIT>" + SERVER_ENDPOINT <EOL> self . logger . writeLog ( syslog . LOG_DEBUG , "<STR_LIT:%s>" % ( str ( log ) ) ) <EOL> myhostname = socket . gethostname ( ) <EOL> externalObject = ExternalObject ( <EOL> buffer = milterContext . fileBuffer , <EOL> externalVars = ExternalVars ( <EOL> filename = milterContext . archiveFileName , <EOL> source = milterContext . milterConfig . milterName + "<STR_LIT:->" + str ( myhostname [ : myhostname . index ( "<STR_LIT:.>" ) ] ) , <EOL> ephID = milterContext . qid , <EOL> uniqID = milterContext . messageID <EOL> ) , <EOL> level = level_metadata <EOL> ) <EOL> result = self . client . send ( externalObject , retry = numRetries , timeout = REQUEST_TIMEOUT ) <EOL> if result : <EOL> self . match = flagRollup ( result ) <EOL> if not self . match : <EOL> self . match = [ ] <EOL> self . attachements = '<STR_LIT:U+002C>' . join ( getAttachmentList ( result ) ) <EOL> strScanResult = finalDispositionFromResult ( result ) <EOL> strScanResults = "<STR_LIT:U+0020>" . join ( dispositionFromResult ( result ) ) <EOL> if strScanResult : <EOL> self . strScanResult = strScanResult <EOL> try : <EOL> self . dispositions = strScanResults <EOL> except : <EOL> self . logger . writeLog ( syslog . LOG_ERR , milterContext . uuid + "<STR_LIT>" ) <EOL> gotResponseFromScanner = <NUM_LIT:1> <EOL> else : <EOL> self . logger . writeLog ( syslog . LOG_ERR , milterContext . uuid + "<STR_LIT:U+0020>" + str ( milterContext . qid ) + "<STR_LIT>" ) <EOL> return gotResponseFromScanner <EOL> class log2syslog ( ) : <EOL> def __init__ ( self , name , facility ) : <EOL> syslog . openlog ( name , <NUM_LIT:0> , facility ) <EOL> def writeLog ( self , logLevel , stringToLog ) : <EOL> syslog . syslog ( logLevel , "<STR_LIT:%s>" % ( str ( stringToLog ) ) ) <EOL> def closeLog ( self ) : <EOL> syslog . closelog ( ) <EOL> class MilterConfig ( ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self ) : <EOL> self . milterName = "<STR_LIT>" <EOL> self . milterInstance = "<STR_LIT>" <EOL> self . socketname = "<STR_LIT>" <EOL> self . servers = [ ] <EOL> self . mode = "<STR_LIT>" <EOL> self . zmqMaxRetry = <NUM_LIT:1> <EOL> self . zmqTimeout = <NUM_LIT> <EOL> self . maxFiles = <NUM_LIT> <EOL> self . heloWhitelist = "<STR_LIT>" <EOL> self . configFileLoc = "<STR_LIT>" <EOL> self . storeEmails = False <EOL> self . storeDir = "<STR_LIT>" <EOL> self . customFolderDateFormat = "<STR_LIT>" <EOL> self . ApplyCustomHeaders = False <EOL> self . CustomHeaderBase = "<STR_LIT>" <EOL> self . ApplyMailscanResultHeader = True <EOL> self . MailscanResultHeaderString = "<STR_LIT>" <EOL> self . loadAttempt = <NUM_LIT:0> <EOL> self . maxLoadAttempts = <NUM_LIT:2> <EOL> self . loadError = False <EOL> self . dispositionModes = { } <EOL> self . dispositionModes [ "<STR_LIT:default>" ] = Milter . CONTINUE <EOL> def loadAll ( self ) : <EOL> self . loadError = False <EOL> self . loadAttempt += <NUM_LIT:1> <EOL> self . loadConfig ( ) <EOL> self . loadScanServers ( ) <EOL> self . loadDispositionModes ( ) <EOL> self . loadHeaderOptions ( ) <EOL> if ( ( self . loadError ) and ( self . loadAttempt <= self . maxLoadAttempts ) ) : <EOL> log = "<STR_LIT>" <EOL> logger = log2syslog ( self . milterName , syslog . LOG_LOCAL0 ) <EOL> logger . writeLog ( syslog . LOG_ERR , "<STR_LIT:%s>" % ( str ( log ) ) ) <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> self . loadAll ( ) <EOL> elif ( ( self . loadError ) and ( self . loadAttempt > self . maxLoadAttempts ) ) : <EOL> log = "<STR_LIT>" <EOL> logger = log2syslog ( self . milterName , syslog . LOG_LOCAL0 ) <EOL> logger . writeLog ( syslog . LOG_ERR , "<STR_LIT:%s>" % ( str ( log ) ) ) <EOL> def loadConfig ( self ) : <EOL> Config = ConfigParser . ConfigParser ( ) <EOL> try : <EOL> Config . read ( self . configFileLoc ) <EOL> self . socketname = Config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . milterName = Config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . milterInstance = Config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . mode = Config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . zmqMaxRetry = int ( Config . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> self . zmqTimeout = int ( Config . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> self . maxFiles = int ( Config . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> self . heloWhitelist = str ( Config . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> self . storeEmails = self . _convertTrueFalse ( Config . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> self . storeDir = Config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . customFolderDateFormat = Config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> except ConfigParser . NoSectionError : <EOL> if ( self . loadAttempt >= self . maxLoadAttempts ) : <EOL> log = "<STR_LIT>" <EOL> logger = log2syslog ( self . milterName , syslog . LOG_LOCAL0 ) <EOL> logger . writeLog ( syslog . LOG_ERR , "<STR_LIT:%s>" % ( str ( log ) ) ) <EOL> else : <EOL> self . loadError = True <EOL> log = "<STR_LIT>" <EOL> logger = log2syslog ( self . milterName , syslog . LOG_LOCAL0 ) <EOL> logger . writeLog ( syslog . LOG_ERR , "<STR_LIT:%s>" % ( str ( log ) ) ) <EOL> def loadDispositionModes ( self ) : <EOL> Config = ConfigParser . ConfigParser ( ) <EOL> try : <EOL> Config . read ( self . configFileLoc ) <EOL> DispositionModes = Config . items ( '<STR_LIT>' ) <EOL> for DispositionMode in DispositionModes : <EOL> self . dispositionModes [ DispositionMode [ <NUM_LIT:0> ] ] = self . _convertDispositionMode ( DispositionMode [ <NUM_LIT:1> ] ) <EOL> except ConfigParser . NoSectionError : <EOL> if ( self . loadAttempt >= self . maxLoadAttempts ) : <EOL> log = "<STR_LIT>" <EOL> logger = log2syslog ( self . milterName , syslog . LOG_LOCAL0 ) <EOL> logger . writeLog ( syslog . LOG_ERR , "<STR_LIT:%s>" % ( str ( log ) ) ) <EOL> else : <EOL> self . loadError = True <EOL> log = "<STR_LIT>" <EOL> logger = log2syslog ( self . milterName , syslog . LOG_LOCAL0 ) <EOL> logger . writeLog ( syslog . LOG_ERR , "<STR_LIT:%s>" % ( str ( log ) ) ) <EOL> def loadHeaderOptions ( self ) : <EOL> Config = ConfigParser . ConfigParser ( ) <EOL> try : <EOL> Config . read ( self . configFileLoc ) <EOL> self . ApplyCustomHeaders = self . _convertTrueFalse ( Config . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> self . CustomHeaderBase = Config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . ApplyMailscanResultHeader = self . _convertTrueFalse ( Config . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> self . MailscanResultHeaderString = Config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> except ConfigParser . NoSectionError : <EOL> if ( self . loadAttempt >= self . maxLoadAttempts ) : <EOL> log = "<STR_LIT>" <EOL> logger = log2syslog ( self . milterName , syslog . LOG_LOCAL0 ) <EOL> logger . writeLog ( syslog . LOG_ERR , "<STR_LIT:%s>" % ( str ( log ) ) ) <EOL> else : <EOL> self . loadError = True <EOL> log = "<STR_LIT>" <EOL> logger = log2syslog ( self . milterName , syslog . LOG_LOCAL0 ) <EOL> logger . writeLog ( syslog . LOG_ERR , "<STR_LIT:%s>" % ( str ( log ) ) ) <EOL> except ConfigParser . NoOptionError : <EOL> if ( self . loadAttempt >= self . maxLoadAttempts ) : <EOL> log = "<STR_LIT>" <EOL> logger = log2syslog ( self . milterName , syslog . LOG_LOCAL0 ) <EOL> logger . writeLog ( syslog . LOG_ERR , "<STR_LIT:%s>" % ( str ( log ) ) ) <EOL> else : <EOL> self . loadError = True <EOL> log = "<STR_LIT>" <EOL> logger = log2syslog ( self . milterName , syslog . LOG_LOCAL0 ) <EOL> logger . writeLog ( syslog . LOG_ERR , "<STR_LIT:%s>" % ( str ( log ) ) ) <EOL> def loadScanServers ( self ) : <EOL> Config = ConfigParser . ConfigParser ( ) <EOL> try : <EOL> Config . read ( self . configFileLoc ) <EOL> servers = Config . items ( '<STR_LIT>' ) <EOL> for server in servers : <EOL> self . servers . append ( server [ <NUM_LIT:1> ] ) <EOL> except ConfigParser . NoSectionError : <EOL> if ( self . loadAttempt >= self . maxLoadAttempts ) : <EOL> log = "<STR_LIT>" <EOL> logger = log2syslog ( self . milterName , syslog . LOG_LOCAL0 ) <EOL> logger . writeLog ( syslog . LOG_ERR , "<STR_LIT:%s>" % ( str ( log ) ) ) <EOL> else : <EOL> self . loadError = True <EOL> log = "<STR_LIT>" <EOL> logger = log2syslog ( self . milterName , syslog . LOG_LOCAL0 ) <EOL> logger . writeLog ( syslog . LOG_ERR , "<STR_LIT:%s>" % ( str ( log ) ) ) <EOL> def _convertDispositionMode ( self , strMode ) : <EOL> convertedMode = Milter . CONTINUE <EOL> if ( strMode == "<STR_LIT>" ) : <EOL> convertedMode = Milter . ACCEPT <EOL> elif ( strMode == "<STR_LIT>" ) : <EOL> convertedMode = Milter . CONTINUE <EOL> elif ( strMode == "<STR_LIT>" ) : <EOL> convertedMode = Milter . REJECT <EOL> elif ( strMode == "<STR_LIT>" ) : <EOL> convertedMode = Milter . DISCARD <EOL> elif ( strMode == "<STR_LIT>" ) : <EOL> convertedMode = Milter . TEMPFAIL <EOL> return convertedMode <EOL> def _unconvertDispositionMode ( self , convertedMode ) : <EOL> '''<STR_LIT>''' <EOL> strMode = "<STR_LIT>" <EOL> if ( convertedMode == Milter . ACCEPT ) : <EOL> strMode = "<STR_LIT>" <EOL> elif ( convertedMode == Milter . CONTINUE ) : <EOL> strMode = "<STR_LIT>" <EOL> elif ( convertedMode == Milter . REJECT ) : <EOL> strMode = "<STR_LIT>" <EOL> elif ( convertedMode == Milter . DISCARD ) : <EOL> strMode = "<STR_LIT>" <EOL> elif ( convertedMode == Milter . TEMPFAIL ) : <EOL> strMode = "<STR_LIT>" <EOL> return strMode <EOL> def _convertTrueFalse ( self , input ) : <EOL> convertedValue = False <EOL> if ( input . upper ( ) == "<STR_LIT>" ) : <EOL> convertedValue = True <EOL> elif ( input . upper == "<STR_LIT>" ) : <EOL> convertedValue = False <EOL> else : <EOL> covnertedValue = "<STR_LIT>" <EOL> return convertedValue <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> random . seed ( ) <EOL> altConfig = None <EOL> if len ( sys . argv ) == <NUM_LIT:2> : <EOL> altConfig = sys . argv [ <NUM_LIT:1> ] <EOL> print "<STR_LIT>" + altConfig <EOL> milterConfig = MilterConfig ( ) <EOL> if altConfig : <EOL> print "<STR_LIT>" % altConfig <EOL> if not os . path . exists ( altConfig ) : <EOL> print "<STR_LIT>" <EOL> else : <EOL> milterConfig . configFileLoc = altConfig <EOL> milterConfig . loadConfig ( ) <EOL> logger = log2syslog ( milterConfig . milterName , syslog . LOG_LOCAL0 ) <EOL> try : <EOL> with open ( milterConfig . heloWhitelist , '<STR_LIT:rb>' ) as f : <EOL> config = json . load ( f ) <EOL> for MTA_group , exclusions_group in config [ '<STR_LIT>' ] . items ( ) : <EOL> for MTA_address in config [ '<STR_LIT>' ] [ MTA_group ] : <EOL> for exclusion in exclusions_group : <EOL> for exclusion_address in config [ '<STR_LIT>' ] [ exclusion ] : <EOL> global_whitelist [ "<STR_LIT>" % ( MTA_address , exclusion_address ) ] = None <EOL> except IOError : <EOL> global_whitelist = { } <EOL> logger . writeLog ( syslog . LOG_ERR , "<STR_LIT>" % ( milterConfig . heloWhitelist ) ) <EOL> except : <EOL> global_whitelist = { } <EOL> logger . writeLog ( syslog . LOG_ERR , "<STR_LIT>" % ( milterConfig . heloWhitelist ) ) <EOL> logger . writeLog ( syslog . LOG_DEBUG , "<STR_LIT>" % ( milterConfig . milterInstance ) ) <EOL> myID = os . getuid ( ) <EOL> grID = os . getgid ( ) <EOL> log = str ( myID ) + "<STR_LIT:U+0020>" + str ( grID ) <EOL> logger . writeLog ( syslog . LOG_DEBUG , "<STR_LIT>" % ( milterConfig . milterInstance , myID , grID ) ) <EOL> maxNumOpenFiles = resource . getrlimit ( resource . RLIMIT_NOFILE ) <EOL> logger . writeLog ( syslog . LOG_DEBUG , "<STR_LIT>" % ( milterConfig . milterInstance , str ( maxNumOpenFiles ) ) ) <EOL> Milter . factory = LaikaMilter <EOL> Milter . set_flags ( Milter . CHGBODY + Milter . CHGHDRS + Milter . ADDHDRS ) <EOL> sys . stdout . flush ( ) <EOL> try : <EOL> Milter . runmilter ( milterConfig . milterName , milterConfig . socketname , <NUM_LIT> ) <EOL> except Milter . error : <EOL> log = "<STR_LIT>" + milterConfig . socketname + "<STR_LIT>" <EOL> logger . writeLog ( syslog . LOG_ERR , "<STR_LIT:%s>" % ( str ( log ) ) ) <EOL> logger . writeLog ( syslog . LOG_DEBUG , "<STR_LIT>" % ( milterConfig . milterInstance ) ) <EOL> logger . closeLog ( ) </s>
<s> from numpy import loadtxt <EOL> from lmfit . models import LorentzianModel , GaussianModel , VoigtModel <EOL> import matplotlib . pyplot as plt <EOL> data = loadtxt ( '<STR_LIT>' ) <EOL> x = data [ : , <NUM_LIT:0> ] <EOL> y = data [ : , <NUM_LIT:1> ] <EOL> MODEL = '<STR_LIT>' <EOL> MODEL = '<STR_LIT>' <EOL> MODEL = '<STR_LIT>' <EOL> gamma_free = True <EOL> if MODEL . lower ( ) . startswith ( '<STR_LIT:g>' ) : <EOL> mod = GaussianModel ( ) <EOL> elif MODEL . lower ( ) . startswith ( '<STR_LIT:l>' ) : <EOL> mod = LorentzianModel ( ) <EOL> elif MODEL . lower ( ) . startswith ( '<STR_LIT:v>' ) : <EOL> mod = VoigtModel ( ) <EOL> pars = mod . guess ( y , x = x ) <EOL> if gamma_free : <EOL> pars [ '<STR_LIT>' ] . set ( value = <NUM_LIT> , vary = True , expr = '<STR_LIT>' ) <EOL> out = mod . fit ( y , pars , x = x ) <EOL> print ( out . fit_report ( min_correl = <NUM_LIT> ) ) <EOL> plt . plot ( x , y , '<STR_LIT>' ) <EOL> plt . plot ( x , out . best_fit , '<STR_LIT>' ) <EOL> plt . show ( ) </s>
<s> import warnings <EOL> has_ipython , has_matplotlib = False , False <EOL> try : <EOL> import matplotlib <EOL> except ImportError : <EOL> pass <EOL> else : <EOL> has_matplotlib = True <EOL> try : <EOL> import IPython <EOL> except ImportError : <EOL> warnings . warn ( "<STR_LIT>" ) <EOL> else : <EOL> _ipy_msg1 = "<STR_LIT>" <EOL> _ipy_msg2 = "<STR_LIT>" <EOL> _ipy_msg3 = "<STR_LIT>" <EOL> try : <EOL> major_version = IPython . release . version_info [ <NUM_LIT:0> ] <EOL> if major_version < <NUM_LIT:2> : <EOL> warnings . warn ( _ipy_msg1 ) <EOL> elif major_version > <NUM_LIT:3> : <EOL> try : <EOL> import ipywidgets <EOL> except ImportError : <EOL> warnings . warn ( _ipy_msg3 ) <EOL> else : <EOL> has_ipython = IPython . get_ipython ( ) is not None <EOL> except Exception as e : <EOL> warnings . warn ( _ipy_msg2 ) <EOL> from . basefitter import BaseFitter <EOL> Fitter = BaseFitter <EOL> if has_matplotlib : <EOL> from . basefitter import MPLFitter <EOL> Fitter = MPLFitter <EOL> if has_ipython : <EOL> from . ipy_fitter import NotebookFitter <EOL> Fitter = NotebookFitter </s>
<s> import climate <EOL> import matplotlib . pyplot as plt <EOL> import numpy as np <EOL> import theanets <EOL> from utils import load_cifar , plot_layers , plot_images <EOL> logging = climate . get_logger ( '<STR_LIT>' ) <EOL> g = climate . add_group ( '<STR_LIT>' ) <EOL> g . add_argument ( '<STR_LIT>' , type = int , default = <NUM_LIT:0> , metavar = '<STR_LIT:N>' , <EOL> help = '<STR_LIT>' ) <EOL> K = <NUM_LIT> <EOL> def pca ( dataset ) : <EOL> mean = dataset [ : <NUM_LIT> ] . mean ( axis = <NUM_LIT:0> ) <EOL> logging . info ( '<STR_LIT>' ) <EOL> x = dataset [ : <NUM_LIT> ] - mean <EOL> vals , vecs = np . linalg . eigh ( np . dot ( x . T , x ) / len ( x ) ) <EOL> vals = vals [ : : - <NUM_LIT:1> ] <EOL> vecs = vecs [ : , : : - <NUM_LIT:1> ] <EOL> vals = np . sqrt ( vals [ : K ] ) <EOL> vecs = vecs [ : , : K ] <EOL> def whiten ( x ) : <EOL> return np . dot ( x , np . dot ( vecs , np . diag ( <NUM_LIT:1.> / vals ) ) ) <EOL> def color ( z ) : <EOL> return np . dot ( z , np . dot ( np . diag ( vals ) , vecs . T ) ) <EOL> return whiten , color <EOL> def main ( args ) : <EOL> train , valid , _ = load_cifar ( ) <EOL> whiten , color = pca ( train ) <EOL> feat = args . features or int ( np . sqrt ( <NUM_LIT:2> * K ) ) <EOL> n = theanets . Autoencoder ( [ K , feat ** <NUM_LIT:2> , K ] ) <EOL> n . train ( whiten ( train ) , whiten ( valid ) , input_noise = <NUM_LIT:1> , train_batches = <NUM_LIT> ) <EOL> plot_layers ( [ <EOL> color ( n . find ( '<STR_LIT>' , '<STR_LIT:w>' ) . get_value ( ) . T ) . T , <EOL> color ( n . find ( '<STR_LIT>' , '<STR_LIT:w>' ) . get_value ( ) ) ] , channels = <NUM_LIT:3> ) <EOL> plt . tight_layout ( ) <EOL> plt . show ( ) <EOL> valid = whiten ( valid [ : <NUM_LIT:100> ] ) <EOL> plot_images ( color ( valid ) , <NUM_LIT> , '<STR_LIT>' , channels = <NUM_LIT:3> ) <EOL> plot_images ( color ( n . predict ( valid ) ) , <NUM_LIT> , <EOL> '<STR_LIT>' , channels = <NUM_LIT:3> ) <EOL> plt . tight_layout ( ) <EOL> plt . show ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> climate . call ( main ) </s>
<s> '''<STR_LIT>''' <EOL> from . activations import Activation <EOL> from . feedforward import Autoencoder , Regressor , Classifier <EOL> from . graph import Network <EOL> from . layers import Layer <EOL> from . losses import Loss <EOL> from . regularizers import Regularizer <EOL> from . main import Experiment <EOL> from . import convolution <EOL> from . import recurrent <EOL> from . import regularizers <EOL> __version__ = '<STR_LIT>' </s>
<s> from django . conf import settings <EOL> from django . db import models <EOL> from django . contrib . auth . models import User , AnonymousUser <EOL> from django . contrib . auth . forms import UserChangeForm <EOL> from django . http import ( HttpResponseRedirect , HttpResponse , <EOL> HttpResponseForbidden , HttpResponseNotFound ) <EOL> from django . views . decorators . http import ( require_GET , require_POST , <EOL> require_http_methods ) <EOL> from django . contrib . auth import REDIRECT_FIELD_NAME , login as auth_login , logout as auth_logout <EOL> from django . contrib . auth . decorators import login_required <EOL> from django . shortcuts import get_object_or_404 , render_to_response <EOL> from django . template import RequestContext <EOL> from . models import UserProfile <EOL> from . forms import UserProfileEditForm , UserEditForm <EOL> try : <EOL> from commons . urlresolvers import reverse <EOL> except ImportError , e : <EOL> from django . core . urlresolvers import reverse <EOL> try : <EOL> from tower import ugettext_lazy as _ <EOL> except ImportError , e : <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> def home ( request ) : <EOL> """<STR_LIT>""" <EOL> if not request . user . is_authenticated ( ) : <EOL> return HttpResponseRedirect ( settings . LOGIN_REDIRECT_URL_FAILURE ) <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' , <EOL> args = ( request . user . username , ) ) ) <EOL> @ require_GET <EOL> def logout ( request ) : <EOL> """<STR_LIT>""" <EOL> auth_logout ( request ) <EOL> return HttpResponseRedirect ( '<STR_LIT:/>' ) <EOL> @ require_GET <EOL> def profile_view ( request , username ) : <EOL> user = get_object_or_404 ( User , username = username ) <EOL> profile = user . get_profile ( ) <EOL> return render_to_response ( '<STR_LIT>' , dict ( <EOL> user = user , profile = profile <EOL> ) , context_instance = RequestContext ( request ) ) <EOL> @ require_http_methods ( [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> @ login_required <EOL> def profile_edit ( request , username ) : <EOL> user = get_object_or_404 ( User , username = username ) <EOL> profile = user . get_profile ( ) <EOL> if not profile . allows_edit ( request . user ) : <EOL> return HttpResponseForbidden ( ) <EOL> user_form = None <EOL> if request . method != "<STR_LIT:POST>" : <EOL> if profile . can_change_username ( user ) : <EOL> user_form = UserEditForm ( profile = profile ) <EOL> profile_form = UserProfileEditForm ( instance = profile ) <EOL> else : <EOL> username = profile . user . username <EOL> is_valid = True <EOL> if profile . can_change_username ( user ) : <EOL> user_form = UserEditForm ( request . POST , profile = profile ) <EOL> if user_form . is_valid ( ) : <EOL> username = user_form . cleaned_data [ '<STR_LIT:username>' ] <EOL> profile . change_username ( username ) <EOL> else : <EOL> is_valid = False <EOL> profile_form = UserProfileEditForm ( request . POST , request . FILES , <EOL> instance = profile ) <EOL> if is_valid and profile_form . is_valid ( ) : <EOL> profile = profile_form . save ( commit = False ) <EOL> profile . save ( ) <EOL> profile_form . save_m2m ( ) <EOL> else : <EOL> is_valid = False <EOL> if is_valid : <EOL> return HttpResponseRedirect ( reverse ( <EOL> '<STR_LIT>' , args = ( username , ) ) ) <EOL> return render_to_response ( '<STR_LIT>' , dict ( <EOL> user_form = user_form , profile_form = profile_form , <EOL> user = user , profile = profile , <EOL> ) , context_instance = RequestContext ( request ) ) </s>
<s> from mbcharsetprober import MultiByteCharSetProber <EOL> from codingstatemachine import CodingStateMachine <EOL> from chardistribution import EUCTWDistributionAnalysis <EOL> from mbcssm import EUCTWSMModel <EOL> class EUCTWProber ( MultiByteCharSetProber ) : <EOL> def __init__ ( self ) : <EOL> MultiByteCharSetProber . __init__ ( self ) <EOL> self . _mCodingSM = CodingStateMachine ( EUCTWSMModel ) <EOL> self . _mDistributionAnalyzer = EUCTWDistributionAnalysis ( ) <EOL> self . reset ( ) <EOL> def get_charset_name ( self ) : <EOL> return "<STR_LIT>" </s>
<s> """<STR_LIT>""" <EOL> SCHEMAS = [ '<STR_LIT:http>' , '<STR_LIT>' ] <EOL> from . import __version__ <EOL> defaults = dict ( ) <EOL> defaults [ '<STR_LIT>' ] = { <EOL> '<STR_LIT>' : '<STR_LIT>' % __version__ , <EOL> '<STR_LIT>' : '<STR_LIT:U+002CU+0020>' . join ( ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> defaults [ '<STR_LIT>' ] = None <EOL> defaults [ '<STR_LIT>' ] = <NUM_LIT:30> <EOL> defaults [ '<STR_LIT>' ] = <NUM_LIT:10> <EOL> defaults [ '<STR_LIT>' ] = <NUM_LIT:10> <EOL> defaults [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> defaults [ '<STR_LIT>' ] = False <EOL> defaults [ '<STR_LIT>' ] = False <EOL> defaults [ '<STR_LIT>' ] = True <EOL> defaults [ '<STR_LIT>' ] = True <EOL> defaults [ '<STR_LIT>' ] = True </s>
<s> from django import forms <EOL> from taggit . tests . models import Food , DirectFood , CustomPKFood , OfficialFood <EOL> class FoodForm ( forms . ModelForm ) : <EOL> class Meta : <EOL> model = Food <EOL> class DirectFoodForm ( forms . ModelForm ) : <EOL> class Meta : <EOL> model = DirectFood <EOL> class CustomPKFoodForm ( forms . ModelForm ) : <EOL> class Meta : <EOL> model = CustomPKFood <EOL> class OfficialFoodForm ( forms . ModelForm ) : <EOL> class Meta : <EOL> model = OfficialFood </s>
<s> __author__ = '<STR_LIT>' <EOL> import sys <EOL> import re <EOL> from math import log10 <EOL> if sys . version [ <NUM_LIT:0> ] == '<STR_LIT:3>' : <EOL> pass <EOL> else : <EOL> range = xrange <EOL> classdef_regex = re . compile ( r"<STR_LIT>" ) <EOL> tagged_line_regex = re . compile ( r"<STR_LIT>" ) <EOL> def convert_time_units ( t ) : <EOL> """<STR_LIT>""" <EOL> order = log10 ( t ) <EOL> if - <NUM_LIT:9> < order < - <NUM_LIT:6> : <EOL> time_units = '<STR_LIT>' <EOL> factor = <NUM_LIT> <EOL> elif - <NUM_LIT:6> < order < - <NUM_LIT:3> : <EOL> time_units = '<STR_LIT>' <EOL> factor = <NUM_LIT> <EOL> elif - <NUM_LIT:3> <= order < - <NUM_LIT:1> : <EOL> time_units = '<STR_LIT>' <EOL> factor = <NUM_LIT> <EOL> elif - <NUM_LIT:1> <= order : <EOL> time_units = '<STR_LIT:s>' <EOL> factor = <NUM_LIT:1> <EOL> return "<STR_LIT>" . format ( factor * t , time_units ) <EOL> def globalize_indentation ( src ) : <EOL> """<STR_LIT>""" <EOL> lines = src . splitlines ( ) <EOL> indent = len ( lines [ <NUM_LIT:0> ] ) - len ( lines [ <NUM_LIT:0> ] . strip ( '<STR_LIT:U+0020>' ) ) <EOL> func_src = '<STR_LIT>' <EOL> for ii , l in enumerate ( src . splitlines ( ) ) : <EOL> line = l [ indent : ] <EOL> func_src += line + '<STR_LIT:\n>' <EOL> return func_src <EOL> def remove_decorators ( src ) : <EOL> """<STR_LIT>""" <EOL> src = src . strip ( ) <EOL> src_lines = src . splitlines ( ) <EOL> multi_line = False <EOL> n_deleted = <NUM_LIT:0> <EOL> for n in range ( len ( src_lines ) ) : <EOL> line = src_lines [ n - n_deleted ] . strip ( ) <EOL> if ( line . startswith ( '<STR_LIT:@>' ) and '<STR_LIT>' in line ) or multi_line : <EOL> del src_lines [ n - n_deleted ] <EOL> n_deleted += <NUM_LIT:1> <EOL> if line . endswith ( '<STR_LIT:)>' ) : <EOL> multi_line = False <EOL> else : <EOL> multi_line = True <EOL> setup_src = '<STR_LIT:\n>' . join ( src_lines ) <EOL> return setup_src <EOL> def get_tagged_imports ( fp ) : <EOL> imports = [ ] <EOL> inside_def = False <EOL> def_lines = [ ] <EOL> def_indent = <NUM_LIT:0> <EOL> with open ( fp , '<STR_LIT:r>' ) as f : <EOL> lastLine = f . readline ( ) <EOL> for line in f : <EOL> tagged_class_or_def = re . findall ( classdef_regex , lastLine ) <EOL> tagged_line = re . findall ( tagged_line_regex , lastLine ) <EOL> if tagged_class_or_def or inside_def : <EOL> if tagged_class_or_def and def_lines : <EOL> imports . append ( '<STR_LIT>' . join ( def_lines ) ) <EOL> def_lines = [ ] <EOL> inside_def = False <EOL> if inside_def : <EOL> indent = len ( lastLine ) - len ( lastLine . lstrip ( '<STR_LIT:U+0020>' ) ) <EOL> if indent == def_indent and lastLine != '<STR_LIT:\n>' : <EOL> imports . append ( '<STR_LIT>' . join ( def_lines ) ) <EOL> def_lines = [ ] <EOL> inside_def = False <EOL> def_indent = <NUM_LIT:0> <EOL> if tagged_line : <EOL> imports . append ( lastLine ) <EOL> else : <EOL> if lastLine != '<STR_LIT:\n>' : <EOL> def_lines . append ( lastLine ) <EOL> else : <EOL> inside_def = True <EOL> def_indent = len ( lastLine ) - len ( lastLine . lstrip ( '<STR_LIT:U+0020>' ) ) <EOL> def_lines . append ( lastLine ) <EOL> elif tagged_line : <EOL> imports . append ( lastLine ) <EOL> lastLine = line <EOL> tagged_line = re . findall ( tagged_line_regex , lastLine ) <EOL> if inside_def : <EOL> def_lines . append ( line ) <EOL> imports . append ( '<STR_LIT>' . join ( def_lines ) ) <EOL> elif tagged_line : <EOL> imports . append ( line ) <EOL> src = '<STR_LIT:\n>' . join ( imports ) + '<STR_LIT:\n>' <EOL> return src <EOL> def generate_call_statement ( func , is_class_method , * args , ** kwargs ) : <EOL> if is_class_method : <EOL> stmt = '<STR_LIT>' + func . __name__ + '<STR_LIT:(>' <EOL> else : <EOL> stmt = func . __name__ + '<STR_LIT:(>' <EOL> for arg in args : <EOL> stmt += arg . __repr__ ( ) + '<STR_LIT:U+002CU+0020>' <EOL> for kw , val in kwargs . items ( ) : <EOL> stmt += '<STR_LIT>' . format ( kw , val . __repr__ ( ) ) <EOL> stmt = stmt . strip ( '<STR_LIT:U+002CU+0020>' ) <EOL> stmt += '<STR_LIT:)>' <EOL> return stmt </s>
<s> from pysphere import VIServer , VITask <EOL> from pysphere . vi_property import VIProperty <EOL> from pysphere . vi_virtual_machine import VIVirtualMachine <EOL> from pysphere . resources import VimService_services as VI <EOL> from pysphere . resources import VimService_services_types as VITypes <EOL> from simplestack . exceptions import HypervisorError <EOL> import re <EOL> import uuid <EOL> VCdIsoBackingInfo = VITypes . ns0 . VirtualCdromIsoBackingInfo_Def ( None ) . pyclass <EOL> VirtualMachineVMIROM = VITypes . ns0 . VirtualMachineVMIROM_Def ( None ) . pyclass <EOL> def get_vm_by_uuid ( server , guest_id ) : <EOL> request = VI . FindByUuidRequestMsg ( ) <EOL> mor_search_index = request . new__this ( <EOL> server . _do_service_content . SearchIndex <EOL> ) <EOL> mor_search_index . set_attribute_type ( '<STR_LIT>' ) <EOL> request . set_element__this ( mor_search_index ) <EOL> request . set_element_uuid ( guest_id ) <EOL> request . set_element_vmSearch ( True ) <EOL> vm = server . _proxy . FindByUuid ( request ) . _returnval <EOL> if vm is not None : <EOL> return VIVirtualMachine ( server , vm ) <EOL> def update_vm ( server , vm_obj , guestdata ) : <EOL> new_annotation = guestdata . get ( "<STR_LIT>" ) <EOL> new_cpus = guestdata . get ( "<STR_LIT>" ) <EOL> new_hdd = guestdata . get ( "<STR_LIT>" ) <EOL> new_iso = guestdata . get ( "<STR_LIT>" ) <EOL> new_memory = guestdata . get ( "<STR_LIT>" ) <EOL> new_name = guestdata . get ( "<STR_LIT:name>" ) <EOL> enable_vmi = guestdata . get ( "<STR_LIT>" ) <EOL> request = VI . ReconfigVM_TaskRequestMsg ( ) <EOL> _this = request . new__this ( vm_obj . _mor ) <EOL> _this . set_attribute_type ( vm_obj . _mor . get_attribute_type ( ) ) <EOL> request . set_element__this ( _this ) <EOL> spec = request . new_spec ( ) <EOL> if new_name and vm_obj . properties . config . name != new_name : <EOL> spec . set_element_name ( new_name ) <EOL> if new_memory and vm_obj . properties . config . hardware . memoryMB != new_memory : <EOL> spec . set_element_memoryMB ( new_memory ) <EOL> if new_cpus and vm_obj . properties . config . hardware . numCPU != new_cpus : <EOL> spec . set_element_numCPUs ( new_cpus ) <EOL> device_config_specs = [ ] <EOL> if new_hdd : <EOL> disk_size = get_disk_size ( vm_obj ) <EOL> if new_hdd * <NUM_LIT> * <NUM_LIT> > disk_size : <EOL> disk = get_disks ( vm_obj ) [ - <NUM_LIT:1> ] <EOL> hdd_in_GB = new_hdd * <NUM_LIT> * <NUM_LIT> <EOL> new_disk_size = hdd_in_GB - disk_size + disk . capacityInKB <EOL> device_config_spec = spec . new_deviceChange ( ) <EOL> device_config_spec . set_element_operation ( '<STR_LIT>' ) <EOL> disk . _obj . set_element_capacityInKB ( new_disk_size ) <EOL> device_config_spec . set_element_device ( disk . _obj ) <EOL> device_config_specs . append ( device_config_spec ) <EOL> if new_iso : <EOL> media_device = get_cd ( vm_obj ) <EOL> connectable = media_device . _obj . new_connectable ( ) <EOL> connectable . set_element_allowGuestControl ( False ) <EOL> if new_iso . get ( "<STR_LIT:name>" ) and new_iso [ "<STR_LIT:name>" ] != "<STR_LIT>" : <EOL> connectable . set_element_connected ( True ) <EOL> connectable . set_element_startConnected ( True ) <EOL> media_device . _obj . set_element_connectable ( connectable ) <EOL> backing = VCdIsoBackingInfo ( ) <EOL> backing . set_element_fileName ( new_iso [ "<STR_LIT:name>" ] ) <EOL> media_device . _obj . set_element_backing ( backing ) <EOL> else : <EOL> connectable . set_element_connected ( False ) <EOL> connectable . set_element_startConnected ( False ) <EOL> media_device . _obj . set_element_connectable ( connectable ) <EOL> device_config_spec = spec . new_deviceChange ( ) <EOL> device_config_spec . set_element_operation ( '<STR_LIT>' ) <EOL> device_config_spec . set_element_device ( media_device . _obj ) <EOL> device_config_specs . append ( device_config_spec ) <EOL> if enable_vmi is not None : <EOL> vmi_driver = get_vmi_driver ( vm_obj ) <EOL> if enable_vmi and not vmi_driver : <EOL> vmi_driver = VirtualMachineVMIROM ( ) <EOL> vmi_driver . set_element_key ( <NUM_LIT> ) <EOL> device_config_spec = spec . new_deviceChange ( ) <EOL> device_config_spec . set_element_operation ( '<STR_LIT>' ) <EOL> device_config_spec . set_element_device ( vmi_driver ) <EOL> device_config_specs . append ( device_config_spec ) <EOL> elif not enable_vmi and vmi_driver : <EOL> device_config_spec = spec . new_deviceChange ( ) <EOL> device_config_spec . set_element_operation ( '<STR_LIT>' ) <EOL> device_config_spec . set_element_device ( vmi_driver . _obj ) <EOL> device_config_specs . append ( device_config_spec ) <EOL> if len ( device_config_specs ) != <NUM_LIT:0> : <EOL> spec . set_element_deviceChange ( device_config_specs ) <EOL> if new_annotation : <EOL> spec . set_element_annotation ( "<STR_LIT:\n>" . join ( new_annotation ) ) <EOL> request . set_element_spec ( spec ) <EOL> ret = server . _proxy . ReconfigVM_Task ( request ) . _returnval <EOL> task = VITask ( ret , server ) <EOL> status = task . wait_for_state ( [ task . STATE_SUCCESS , task . STATE_ERROR ] ) <EOL> if status != task . STATE_SUCCESS : <EOL> raise HypervisorError ( "<STR_LIT>" % task . get_error_message ( ) ) <EOL> def enable_vmi ( server , vm_obj ) : <EOL> request = VI . ReconfigVM_TaskRequestMsg ( ) <EOL> _this = request . new__this ( vm_obj . _mor ) <EOL> _this . set_attribute_type ( vm_obj . _mor . get_attribute_type ( ) ) <EOL> request . set_element__this ( _this ) <EOL> spec = request . new_spec ( ) <EOL> device_config_specs = [ ] <EOL> media_device = VirtualMachineVMIROM ( ) <EOL> media_device . set_element_key ( <NUM_LIT> ) <EOL> device_config_spec = spec . new_deviceChange ( ) <EOL> device_config_spec . set_element_operation ( '<STR_LIT>' ) <EOL> device_config_spec . set_element_device ( media_device ) <EOL> device_config_specs . append ( device_config_spec ) <EOL> spec . set_element_deviceChange ( device_config_specs ) <EOL> request . set_element_spec ( spec ) <EOL> ret = server . _proxy . ReconfigVM_Task ( request ) . _returnval <EOL> task = VITask ( ret , server ) <EOL> status = task . wait_for_state ( [ task . STATE_SUCCESS , task . STATE_ERROR ] ) <EOL> if status != task . STATE_SUCCESS : <EOL> raise HypervisorError ( "<STR_LIT>" % task . get_error_message ( ) ) <EOL> def export ( server , vm_obj ) : <EOL> request = VI . ExportVmRequestMsg ( ) <EOL> _this = request . new__this ( vm_obj . _mor ) <EOL> _this . set_attribute_type ( vm_obj . _mor . get_attribute_type ( ) ) <EOL> request . set_element__this ( _this ) <EOL> ret = server . _proxy . ExportVm ( request ) <EOL> mor = ret . _returnval <EOL> http = VIProperty ( connection , ret . _returnval ) <EOL> url = http . info . deviceUrl [ <NUM_LIT:0> ] . url <EOL> request = VI . HttpNfcLeaseProgressRequestMsg ( ) <EOL> _this = request . new__this ( mor ) <EOL> _this . set_attribute_type ( MORTypes . HttpNfcLease ) <EOL> request . set_element__this ( _this ) <EOL> request . set_element_percent ( <NUM_LIT:100> ) <EOL> server . _proxy . HttpNfcLeaseProgress ( request ) <EOL> request = VI . HttpNfcLeaseCompleteRequestMsg ( ) <EOL> _this = request . new__this ( mor ) <EOL> _this . set_attribute_type ( MORTypes . HttpNfcLease ) <EOL> request . set_element__this ( _this ) <EOL> server . _proxy . HttpNfcLeaseComplete ( request ) <EOL> def delete_vm ( server , vm_obj ) : <EOL> request = VI . Destroy_TaskRequestMsg ( ) <EOL> _this = request . new__this ( vm_obj . _mor ) <EOL> _this . set_attribute_type ( vm_obj . _mor . get_attribute_type ( ) ) <EOL> request . set_element__this ( _this ) <EOL> ret = server . _proxy . Destroy_Task ( request ) . _returnval <EOL> task = VITask ( ret , server ) <EOL> status = task . wait_for_state ( [ task . STATE_SUCCESS , task . STATE_ERROR ] ) <EOL> if status != task . STATE_SUCCESS : <EOL> raise HypervisorError ( "<STR_LIT>" % task . get_error_message ( ) ) <EOL> def get_tags ( vm_obj ) : <EOL> return vm_obj . properties . config . annotation . splitlines ( ) <EOL> def delete_tag ( tag_name , vm_obj ) : <EOL> tags = get_tags ( vm_obj ) <EOL> for tag in tags [ : ] : <EOL> if tag == tag_name : <EOL> tags . remove ( tag_name ) <EOL> return tags <EOL> def create_tag ( tag , vm_obj ) : <EOL> tags = get_tags ( vm_obj ) <EOL> tags . append ( tag ) <EOL> return tags <EOL> def get_vmi_driver ( vm_obj ) : <EOL> for device in vm_obj . properties . config . hardware . device : <EOL> if device . _type == "<STR_LIT>" : <EOL> return device <EOL> return None <EOL> def get_cd ( vm_obj ) : <EOL> for device in vm_obj . properties . config . hardware . device : <EOL> if device . _type == "<STR_LIT>" : <EOL> return device <EOL> def get_disks ( vm_obj ) : <EOL> disks = [ ] <EOL> for device in vm_obj . properties . config . hardware . device : <EOL> if device . _type == "<STR_LIT>" : <EOL> disks . append ( device ) <EOL> return disks <EOL> def get_disk_size ( vm_obj ) : <EOL> size = <NUM_LIT:0> <EOL> for disk in get_disks ( vm_obj ) : <EOL> size += disk . capacityInKB <EOL> return size <EOL> def get_network_interfaces ( vm_obj ) : <EOL> vif_types = [ <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" <EOL> ] <EOL> vifs = [ ] <EOL> for device in vm_obj . properties . config . hardware . device : <EOL> if device . _type in vif_types : <EOL> vifs . append ( device ) <EOL> return vifs <EOL> def create_snapshot ( server , vm_obj , snapshot_name ) : <EOL> snapshot_id = str ( uuid . uuid4 ( ) ) <EOL> request = VI . CreateSnapshot_TaskRequestMsg ( ) <EOL> _this = request . new__this ( vm_obj . _mor ) <EOL> _this . set_attribute_type ( vm_obj . _mor . get_attribute_type ( ) ) <EOL> request . set_element__this ( _this ) <EOL> request . set_element_name ( snapshot_name ) <EOL> request . set_element_description ( snapshot_id ) <EOL> request . set_element_memory ( False ) <EOL> request . set_element_quiesce ( False ) <EOL> ret = server . _proxy . CreateSnapshot_Task ( request ) . _returnval <EOL> task = VITask ( ret , server ) <EOL> status = task . wait_for_state ( [ task . STATE_SUCCESS , task . STATE_ERROR ] ) <EOL> if status != task . STATE_SUCCESS : <EOL> raise HypervisorError ( "<STR_LIT>" % task . get_error_message ( ) ) <EOL> vm_obj . refresh_snapshot_list ( ) <EOL> return get_snapshot ( vm_obj , snapshot_id ) <EOL> def get_snapshot ( vm_obj , snapshot_id ) : <EOL> regex = r'<STR_LIT>' <EOL> if re . match ( regex , snapshot_id , re . I ) : <EOL> for snap in vm_obj . get_snapshots ( ) : <EOL> if snap . get_description ( ) == snapshot_id : <EOL> return snap <EOL> else : <EOL> for snap in vm_obj . get_snapshots ( ) : <EOL> if snap . get_name ( ) == snapshot_id : <EOL> return snap <EOL> def revert_to_snapshot ( server , vm_obj , snapshot_obj ) : <EOL> request = VI . RevertToSnapshot_TaskRequestMsg ( ) <EOL> mor_snap = request . new__this ( snapshot_obj . _mor ) <EOL> mor_snap . set_attribute_type ( snapshot_obj . _mor . get_attribute_type ( ) ) <EOL> request . set_element__this ( mor_snap ) <EOL> ret = server . _proxy . RevertToSnapshot_Task ( request ) . _returnval <EOL> task = VITask ( ret , server ) <EOL> status = task . wait_for_state ( [ task . STATE_SUCCESS , task . STATE_ERROR ] ) <EOL> if status != task . STATE_SUCCESS : <EOL> raise HypervisorError ( "<STR_LIT>" % task . get_error_message ( ) ) <EOL> def delete_snapshot ( server , vm_obj , snapshot_obj , remove_children = False ) : <EOL> request = VI . RemoveSnapshot_TaskRequestMsg ( ) <EOL> mor_snap = request . new__this ( snapshot_obj . _mor ) <EOL> mor_snap . set_attribute_type ( snapshot_obj . _mor . get_attribute_type ( ) ) <EOL> request . set_element__this ( mor_snap ) <EOL> request . set_element_removeChildren ( remove_children ) <EOL> ret = server . _proxy . RemoveSnapshot_Task ( request ) . _returnval <EOL> task = VITask ( ret , server ) <EOL> status = task . wait_for_state ( [ task . STATE_SUCCESS , task . STATE_ERROR ] ) <EOL> vm_obj . refresh_snapshot_list ( ) <EOL> if status != task . STATE_SUCCESS : <EOL> raise HypervisorError ( "<STR_LIT>" % task . get_error_message ( ) ) </s>
<s> """<STR_LIT>""" <EOL> from dfvfs . analyzer import analyzer <EOL> from dfvfs . analyzer import analyzer_helper <EOL> from dfvfs . analyzer import specification <EOL> from dfvfs . lib import definitions <EOL> class VMDKAnalyzerHelper ( analyzer_helper . AnalyzerHelper ) : <EOL> """<STR_LIT>""" <EOL> FORMAT_CATEGORIES = frozenset ( [ <EOL> definitions . FORMAT_CATEGORY_STORAGE_MEDIA_IMAGE ] ) <EOL> TYPE_INDICATOR = definitions . TYPE_INDICATOR_VMDK <EOL> def GetFormatSpecification ( self ) : <EOL> """<STR_LIT>""" <EOL> format_specification = specification . FormatSpecification ( <EOL> self . type_indicator ) <EOL> format_specification . AddNewSignature ( b'<STR_LIT>' , offset = <NUM_LIT:0> ) <EOL> format_specification . AddNewSignature ( b'<STR_LIT>' , offset = <NUM_LIT:0> ) <EOL> format_specification . AddNewSignature ( b'<STR_LIT>' , offset = <NUM_LIT:0> ) <EOL> return format_specification <EOL> analyzer . Analyzer . RegisterHelper ( VMDKAnalyzerHelper ( ) ) </s>
<s> """<STR_LIT>""" <EOL> from Crypto . Cipher import ARC4 <EOL> from dfvfs . encryption import decrypter <EOL> from dfvfs . encryption import manager <EOL> from dfvfs . lib import definitions <EOL> class RC4Decrypter ( decrypter . Decrypter ) : <EOL> """<STR_LIT>""" <EOL> ENCRYPTION_METHOD = definitions . ENCRYPTION_METHOD_RC4 <EOL> def __init__ ( self , key = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not key : <EOL> raise ValueError ( u'<STR_LIT>' ) <EOL> super ( RC4Decrypter , self ) . __init__ ( ) <EOL> self . _rc4_cipher = ARC4 . new ( key ) <EOL> def Decrypt ( self , encrypted_data ) : <EOL> """<STR_LIT>""" <EOL> decrypted_data = self . _rc4_cipher . decrypt ( encrypted_data ) <EOL> return decrypted_data , b'<STR_LIT>' <EOL> manager . EncryptionManager . RegisterDecrypter ( RC4Decrypter ) </s>
<s> """<STR_LIT>""" <EOL> from dfvfs . analyzer import analyzer <EOL> from dfvfs . lib import definitions <EOL> from dfvfs . lib import errors <EOL> from dfvfs . lib import raw <EOL> from dfvfs . path import factory as path_spec_factory <EOL> from dfvfs . resolver import resolver <EOL> class SourceScanNode ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , path_spec ) : <EOL> """<STR_LIT>""" <EOL> super ( SourceScanNode , self ) . __init__ ( ) <EOL> self . path_spec = path_spec <EOL> self . parent_node = None <EOL> self . scanned = False <EOL> self . sub_nodes = [ ] <EOL> @ property <EOL> def type_indicator ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . path_spec . type_indicator <EOL> def GetSubNodeByLocation ( self , location ) : <EOL> """<STR_LIT>""" <EOL> for sub_node in self . sub_nodes : <EOL> sub_node_location = getattr ( sub_node . path_spec , u'<STR_LIT:location>' , u'<STR_LIT>' ) <EOL> if location == sub_node_location : <EOL> return sub_node <EOL> def GetUnscannedSubNode ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . sub_nodes and not self . scanned : <EOL> return self <EOL> for sub_node in self . sub_nodes : <EOL> result = sub_node . GetUnscannedSubNode ( ) <EOL> if result : <EOL> return result <EOL> def IsSystemLevel ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . path_spec . IsSystemLevel ( ) <EOL> class SourceScannerContext ( object ) : <EOL> """<STR_LIT>""" <EOL> SOURCE_TYPE_DIRECTORY = definitions . SOURCE_TYPE_DIRECTORY <EOL> SOURCE_TYPE_FILE = definitions . SOURCE_TYPE_FILE <EOL> SOURCE_TYPE_STORAGE_MEDIA_DEVICE = ( <EOL> definitions . SOURCE_TYPE_STORAGE_MEDIA_DEVICE ) <EOL> SOURCE_TYPE_STORAGE_MEDIA_IMAGE = ( <EOL> definitions . SOURCE_TYPE_STORAGE_MEDIA_IMAGE ) <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> super ( SourceScannerContext , self ) . __init__ ( ) <EOL> self . _file_system_scan_nodes = { } <EOL> self . _locked_scan_nodes = { } <EOL> self . _root_path_spec = None <EOL> self . _scan_nodes = { } <EOL> self . source_type = None <EOL> self . updated = False <EOL> @ property <EOL> def locked_scan_nodes ( self ) : <EOL> """<STR_LIT>""" <EOL> return iter ( self . _locked_scan_nodes . values ( ) ) <EOL> def AddScanNode ( self , path_spec , parent_scan_node ) : <EOL> """<STR_LIT>""" <EOL> scan_node = self . _scan_nodes . get ( path_spec , None ) <EOL> if scan_node : <EOL> raise KeyError ( u'<STR_LIT>' ) <EOL> scan_node = SourceScanNode ( path_spec ) <EOL> if parent_scan_node : <EOL> if parent_scan_node . path_spec not in self . _scan_nodes : <EOL> raise RuntimeError ( u'<STR_LIT>' ) <EOL> scan_node . parent_node = parent_scan_node <EOL> parent_scan_node . sub_nodes . append ( scan_node ) <EOL> if not self . _root_path_spec : <EOL> self . _root_path_spec = path_spec <EOL> self . _scan_nodes [ path_spec ] = scan_node <EOL> if path_spec . type_indicator in definitions . FILE_SYSTEM_TYPE_INDICATORS : <EOL> self . _file_system_scan_nodes [ path_spec ] = scan_node <EOL> self . updated = True <EOL> return scan_node <EOL> def HasFileSystemScanNodes ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _file_system_scan_nodes != { } <EOL> def HasLockedScanNodes ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _locked_scan_nodes != { } <EOL> def HasScanNode ( self , path_spec ) : <EOL> """<STR_LIT>""" <EOL> return self . _scan_nodes . get ( path_spec , None ) is not None <EOL> def GetRootScanNode ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _scan_nodes . get ( self . _root_path_spec , None ) <EOL> def GetScanNode ( self , path_spec ) : <EOL> """<STR_LIT>""" <EOL> return self . _scan_nodes . get ( path_spec , None ) <EOL> def GetUnscannedScanNode ( self ) : <EOL> """<STR_LIT>""" <EOL> root_scan_node = self . _scan_nodes . get ( self . _root_path_spec , None ) <EOL> if not root_scan_node . scanned : <EOL> return root_scan_node <EOL> return root_scan_node . GetUnscannedSubNode ( ) <EOL> def IsLockedScanNode ( self , path_spec ) : <EOL> """<STR_LIT>""" <EOL> return path_spec in self . _locked_scan_nodes <EOL> def IsSourceTypeDirectory ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . source_type : <EOL> return self . source_type == definitions . SOURCE_TYPE_DIRECTORY <EOL> def IsSourceTypeFile ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . source_type : <EOL> return self . source_type == definitions . SOURCE_TYPE_FILE <EOL> def LockScanNode ( self , path_spec ) : <EOL> """<STR_LIT>""" <EOL> scan_node = self . _scan_nodes . get ( path_spec , None ) <EOL> if not scan_node : <EOL> raise KeyError ( u'<STR_LIT>' ) <EOL> self . _locked_scan_nodes [ path_spec ] = scan_node <EOL> def OpenSourcePath ( self , source_path ) : <EOL> """<STR_LIT>""" <EOL> source_path_spec = path_spec_factory . Factory . NewPathSpec ( <EOL> definitions . TYPE_INDICATOR_OS , location = source_path ) <EOL> self . AddScanNode ( source_path_spec , None ) <EOL> def RemoveScanNode ( self , path_spec ) : <EOL> """<STR_LIT>""" <EOL> scan_node = self . _scan_nodes . get ( path_spec , None ) <EOL> if not scan_node : <EOL> return <EOL> if scan_node . sub_nodes : <EOL> raise RuntimeError ( u'<STR_LIT>' ) <EOL> parent_scan_node = scan_node . parent_node <EOL> if parent_scan_node : <EOL> parent_scan_node . sub_nodes . remove ( scan_node ) <EOL> if path_spec == self . _root_path_spec : <EOL> self . _root_path_spec = None <EOL> del self . _scan_nodes [ path_spec ] <EOL> if path_spec . type_indicator in definitions . FILE_SYSTEM_TYPE_INDICATORS : <EOL> del self . _file_system_scan_nodes [ path_spec ] <EOL> return parent_scan_node <EOL> def SetSourceType ( self , source_type ) : <EOL> """<STR_LIT>""" <EOL> if self . source_type is None : <EOL> self . source_type = source_type <EOL> def UnlockScanNode ( self , path_spec ) : <EOL> """<STR_LIT>""" <EOL> if not self . HasScanNode ( path_spec ) : <EOL> raise KeyError ( u'<STR_LIT>' ) <EOL> del self . _locked_scan_nodes [ path_spec ] <EOL> class SourceScanner ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , resolver_context = None ) : <EOL> """<STR_LIT>""" <EOL> super ( SourceScanner , self ) . __init__ ( ) <EOL> self . _resolver_context = resolver_context <EOL> def _ScanNode ( self , scan_context , scan_node , auto_recurse = True ) : <EOL> """<STR_LIT>""" <EOL> if not scan_context : <EOL> raise ValueError ( u'<STR_LIT>' ) <EOL> if not scan_node : <EOL> raise ValueError ( u'<STR_LIT>' ) <EOL> scan_path_spec = scan_node . path_spec <EOL> if not scan_node . IsSystemLevel ( ) : <EOL> system_level_file_entry = None <EOL> else : <EOL> system_level_file_entry = resolver . Resolver . OpenFileEntry ( <EOL> scan_node . path_spec , resolver_context = self . _resolver_context ) <EOL> if system_level_file_entry is None : <EOL> raise errors . BackEndError ( u'<STR_LIT>' ) <EOL> if system_level_file_entry . IsDirectory ( ) : <EOL> scan_context . SetSourceType ( definitions . SOURCE_TYPE_DIRECTORY ) <EOL> return <EOL> source_path_spec = self . ScanForStorageMediaImage ( scan_node . path_spec ) <EOL> if source_path_spec : <EOL> scan_node . scanned = True <EOL> scan_node = scan_context . AddScanNode ( source_path_spec , scan_node ) <EOL> if system_level_file_entry . IsDevice ( ) : <EOL> scan_context . SetSourceType ( <EOL> definitions . SOURCE_TYPE_STORAGE_MEDIA_DEVICE ) <EOL> else : <EOL> scan_context . SetSourceType ( <EOL> definitions . SOURCE_TYPE_STORAGE_MEDIA_IMAGE ) <EOL> if not auto_recurse : <EOL> return <EOL> source_path_spec = None <EOL> while True : <EOL> if scan_node . type_indicator in definitions . FILE_SYSTEM_TYPE_INDICATORS : <EOL> break <EOL> if scan_context . IsLockedScanNode ( scan_node . path_spec ) : <EOL> break <EOL> source_path_spec = self . ScanForVolumeSystem ( scan_node . path_spec ) <EOL> if not source_path_spec : <EOL> break <EOL> if not scan_context . HasScanNode ( source_path_spec ) : <EOL> scan_node . scanned = True <EOL> scan_node = scan_context . AddScanNode ( source_path_spec , scan_node ) <EOL> if system_level_file_entry and system_level_file_entry . IsDevice ( ) : <EOL> scan_context . SetSourceType ( <EOL> definitions . SOURCE_TYPE_STORAGE_MEDIA_DEVICE ) <EOL> else : <EOL> scan_context . SetSourceType ( <EOL> definitions . SOURCE_TYPE_STORAGE_MEDIA_IMAGE ) <EOL> if scan_node . type_indicator in definitions . VOLUME_SYSTEM_TYPE_INDICATORS : <EOL> if scan_node . type_indicator == definitions . TYPE_INDICATOR_VSHADOW : <EOL> path_spec = self . ScanForFileSystem ( scan_node . path_spec . parent ) <EOL> if path_spec : <EOL> scan_context . AddScanNode ( path_spec , scan_node . parent_node ) <EOL> file_entry = resolver . Resolver . OpenFileEntry ( <EOL> scan_node . path_spec , resolver_context = self . _resolver_context ) <EOL> for sub_file_entry in file_entry . sub_file_entries : <EOL> sub_scan_node = scan_context . AddScanNode ( <EOL> sub_file_entry . path_spec , scan_node ) <EOL> if scan_node . type_indicator != definitions . TYPE_INDICATOR_VSHADOW : <EOL> if auto_recurse or not scan_context . updated : <EOL> self . _ScanNode ( <EOL> scan_context , sub_scan_node , auto_recurse = auto_recurse ) <EOL> return <EOL> elif scan_node . type_indicator in ( <EOL> definitions . ENCRYPTED_VOLUME_TYPE_INDICATORS ) : <EOL> file_object = resolver . Resolver . OpenFileObject ( <EOL> scan_node . path_spec , resolver_context = self . _resolver_context ) <EOL> is_locked = not file_object or file_object . is_locked <EOL> file_object . close ( ) <EOL> if is_locked : <EOL> scan_context . LockScanNode ( scan_node . path_spec ) <EOL> if scan_node . type_indicator == definitions . TYPE_INDICATOR_BDE : <EOL> path_spec = self . ScanForFileSystem ( scan_node . path_spec . parent ) <EOL> if path_spec : <EOL> scan_context . AddScanNode ( path_spec , scan_node . parent_node ) <EOL> if not auto_recurse and scan_context . updated : <EOL> return <EOL> if not scan_context . updated : <EOL> break <EOL> if ( scan_node . path_spec . type_indicator in ( <EOL> definitions . VOLUME_SYSTEM_TYPE_INDICATORS ) and <EOL> getattr ( scan_node . path_spec , u'<STR_LIT:location>' , None ) == u'<STR_LIT:/>' ) : <EOL> pass <EOL> elif scan_context . IsLockedScanNode ( scan_node . path_spec ) : <EOL> pass <EOL> elif ( scan_node . type_indicator == definitions . TYPE_INDICATOR_VSHADOW and <EOL> auto_recurse and scan_node . path_spec != scan_path_spec ) : <EOL> pass <EOL> elif scan_node . type_indicator not in ( <EOL> definitions . FILE_SYSTEM_TYPE_INDICATORS ) : <EOL> source_path_spec = self . ScanForFileSystem ( scan_node . path_spec ) <EOL> if not source_path_spec : <EOL> if scan_node . path_spec . type_indicator == definitions . TYPE_INDICATOR_RAW : <EOL> scan_node = scan_context . RemoveScanNode ( scan_node . path_spec ) <EOL> scan_context . source_type = definitions . SOURCE_TYPE_FILE <EOL> else : <EOL> scan_context . SetSourceType ( definitions . SOURCE_TYPE_FILE ) <EOL> elif not scan_context . HasScanNode ( source_path_spec ) : <EOL> scan_node . scanned = True <EOL> scan_node = scan_context . AddScanNode ( source_path_spec , scan_node ) <EOL> if system_level_file_entry and system_level_file_entry . IsDevice ( ) : <EOL> scan_context . SetSourceType ( <EOL> definitions . SOURCE_TYPE_STORAGE_MEDIA_DEVICE ) <EOL> else : <EOL> scan_context . SetSourceType ( <EOL> definitions . SOURCE_TYPE_STORAGE_MEDIA_IMAGE ) <EOL> if not scan_node . scanned : <EOL> scan_node . scanned = True <EOL> return <EOL> def GetVolumeIdentifiers ( self , volume_system ) : <EOL> """<STR_LIT>""" <EOL> volume_identifiers = [ ] <EOL> for volume in volume_system . volumes : <EOL> volume_identifier = getattr ( volume , u'<STR_LIT>' , None ) <EOL> if volume_identifier : <EOL> volume_identifiers . append ( volume_identifier ) <EOL> return sorted ( volume_identifiers ) <EOL> def Scan ( self , scan_context , auto_recurse = True , scan_path_spec = None ) : <EOL> """<STR_LIT>""" <EOL> if not scan_context : <EOL> raise ValueError ( u'<STR_LIT>' ) <EOL> scan_context . updated = False <EOL> if scan_path_spec : <EOL> scan_node = scan_context . GetScanNode ( scan_path_spec ) <EOL> else : <EOL> scan_node = scan_context . GetUnscannedScanNode ( ) <EOL> if scan_node : <EOL> self . _ScanNode ( scan_context , scan_node , auto_recurse = auto_recurse ) <EOL> def ScanForFileSystem ( self , source_path_spec ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> type_indicators = analyzer . Analyzer . GetFileSystemTypeIndicators ( <EOL> source_path_spec , resolver_context = self . _resolver_context ) <EOL> except RuntimeError as exception : <EOL> raise errors . BackEndError ( ( <EOL> u'<STR_LIT>' <EOL> u'<STR_LIT>' ) . format ( exception ) ) <EOL> if not type_indicators : <EOL> return <EOL> type_indicator = type_indicators [ <NUM_LIT:0> ] <EOL> if len ( type_indicators ) > <NUM_LIT:1> : <EOL> if definitions . PREFERRED_NTFS_BACK_END not in type_indicators : <EOL> raise errors . BackEndError ( <EOL> u'<STR_LIT>' ) <EOL> type_indicator = definitions . PREFERRED_NTFS_BACK_END <EOL> if type_indicator == definitions . TYPE_INDICATOR_NTFS : <EOL> return path_spec_factory . Factory . NewPathSpec ( <EOL> type_indicator , location = u'<STR_LIT:\\>' , parent = source_path_spec ) <EOL> return path_spec_factory . Factory . NewPathSpec ( <EOL> type_indicator , location = u'<STR_LIT:/>' , parent = source_path_spec ) <EOL> def ScanForStorageMediaImage ( self , source_path_spec ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> type_indicators = analyzer . Analyzer . GetStorageMediaImageTypeIndicators ( <EOL> source_path_spec , resolver_context = self . _resolver_context ) <EOL> except RuntimeError as exception : <EOL> raise errors . BackEndError ( ( <EOL> u'<STR_LIT>' <EOL> u'<STR_LIT>' ) . format ( exception ) ) <EOL> if not type_indicators : <EOL> file_system = resolver . Resolver . OpenFileSystem ( <EOL> source_path_spec , resolver_context = self . _resolver_context ) <EOL> raw_path_spec = path_spec_factory . Factory . NewPathSpec ( <EOL> definitions . TYPE_INDICATOR_RAW , parent = source_path_spec ) <EOL> try : <EOL> glob_results = raw . RawGlobPathSpec ( file_system , raw_path_spec ) <EOL> except errors . PathSpecError : <EOL> glob_results = None <EOL> if not glob_results : <EOL> return <EOL> return raw_path_spec <EOL> if len ( type_indicators ) > <NUM_LIT:1> : <EOL> raise errors . BackEndError ( <EOL> u'<STR_LIT>' ) <EOL> return path_spec_factory . Factory . NewPathSpec ( <EOL> type_indicators [ <NUM_LIT:0> ] , parent = source_path_spec ) <EOL> def ScanForVolumeSystem ( self , source_path_spec ) : <EOL> """<STR_LIT>""" <EOL> if source_path_spec . type_indicator == definitions . TYPE_INDICATOR_VSHADOW : <EOL> return <EOL> if source_path_spec . type_indicator in ( <EOL> definitions . VOLUME_SYSTEM_TYPE_INDICATORS ) : <EOL> if getattr ( source_path_spec , u'<STR_LIT:location>' , None ) == u'<STR_LIT:/>' : <EOL> return source_path_spec <EOL> try : <EOL> type_indicators = analyzer . Analyzer . GetVolumeSystemTypeIndicators ( <EOL> source_path_spec , resolver_context = self . _resolver_context ) <EOL> except ( IOError , RuntimeError ) as exception : <EOL> raise errors . BackEndError ( ( <EOL> u'<STR_LIT>' <EOL> u'<STR_LIT>' ) . format ( exception ) ) <EOL> if not type_indicators : <EOL> return <EOL> if len ( type_indicators ) > <NUM_LIT:1> : <EOL> raise errors . BackEndError ( <EOL> u'<STR_LIT>' ) <EOL> if ( type_indicators [ <NUM_LIT:0> ] == definitions . TYPE_INDICATOR_TSK_PARTITION and <EOL> source_path_spec . type_indicator in [ <EOL> definitions . TYPE_INDICATOR_TSK_PARTITION ] ) : <EOL> return <EOL> if type_indicators [ <NUM_LIT:0> ] in definitions . VOLUME_SYSTEM_TYPE_INDICATORS : <EOL> return path_spec_factory . Factory . NewPathSpec ( <EOL> type_indicators [ <NUM_LIT:0> ] , location = u'<STR_LIT:/>' , parent = source_path_spec ) <EOL> return path_spec_factory . Factory . NewPathSpec ( <EOL> type_indicators [ <NUM_LIT:0> ] , parent = source_path_spec ) <EOL> def Unlock ( <EOL> self , scan_context , path_spec , credential_identifier , credential_data ) : <EOL> """<STR_LIT>""" <EOL> if not scan_context . HasScanNode ( path_spec ) : <EOL> raise KeyError ( u'<STR_LIT>' ) <EOL> if not scan_context . IsLockedScanNode ( path_spec ) : <EOL> raise KeyError ( u'<STR_LIT>' ) <EOL> resolver . Resolver . key_chain . SetCredential ( <EOL> path_spec , credential_identifier , credential_data ) <EOL> file_object = resolver . Resolver . OpenFileObject ( <EOL> path_spec , resolver_context = self . _resolver_context ) <EOL> is_locked = not file_object or file_object . is_locked <EOL> file_object . close ( ) <EOL> if is_locked : <EOL> return False <EOL> scan_context . UnlockScanNode ( path_spec ) <EOL> return True </s>
<s> """<STR_LIT>""" <EOL> from dfvfs . lib import definitions <EOL> from dfvfs . path import factory <EOL> from dfvfs . path import location_path_spec <EOL> class FakePathSpec ( location_path_spec . LocationPathSpec ) : <EOL> """<STR_LIT>""" <EOL> _IS_SYSTEM_LEVEL = True <EOL> TYPE_INDICATOR = definitions . TYPE_INDICATOR_FAKE <EOL> def __init__ ( self , location = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> parent = None <EOL> if u'<STR_LIT>' in kwargs : <EOL> parent = kwargs [ u'<STR_LIT>' ] <EOL> del kwargs [ u'<STR_LIT>' ] <EOL> if parent : <EOL> raise ValueError ( u'<STR_LIT>' ) <EOL> super ( FakePathSpec , self ) . __init__ ( <EOL> location = location , parent = parent , ** kwargs ) <EOL> factory . Factory . RegisterPathSpec ( FakePathSpec ) </s>
<s> """<STR_LIT>""" <EOL> import dfvfs . file_io . encrypted_stream_io <EOL> import dfvfs . vfs . encrypted_stream_file_system <EOL> from dfvfs . lib import definitions <EOL> from dfvfs . resolver import resolver <EOL> from dfvfs . resolver import resolver_helper <EOL> class EncryptedStreamResolverHelper ( resolver_helper . ResolverHelper ) : <EOL> """<STR_LIT>""" <EOL> TYPE_INDICATOR = definitions . TYPE_INDICATOR_ENCRYPTED_STREAM <EOL> def NewFileObject ( self , resolver_context ) : <EOL> """<STR_LIT>""" <EOL> return dfvfs . file_io . encrypted_stream_io . EncryptedStream ( resolver_context ) <EOL> def NewFileSystem ( self , resolver_context ) : <EOL> """<STR_LIT>""" <EOL> return dfvfs . vfs . encrypted_stream_file_system . EncryptedStreamFileSystem ( <EOL> resolver_context ) <EOL> resolver . Resolver . RegisterHelper ( EncryptedStreamResolverHelper ( ) ) </s>
<s> """<STR_LIT>""" <EOL> from dfvfs . lib import definitions <EOL> from dfvfs . lib import errors <EOL> from dfvfs . vfs import root_only_file_entry <EOL> from dfvfs . vfs import vfs_stat <EOL> class EncodedStreamFileEntry ( root_only_file_entry . RootOnlyFileEntry ) : <EOL> """<STR_LIT>""" <EOL> TYPE_INDICATOR = definitions . TYPE_INDICATOR_ENCODED_STREAM <EOL> def _GetStat ( self ) : <EOL> """<STR_LIT>""" <EOL> encoded_stream = self . GetFileObject ( ) <EOL> if not encoded_stream : <EOL> raise errors . BackEndError ( <EOL> u'<STR_LIT>' . format ( <EOL> self . path_spec . comparable ) ) <EOL> try : <EOL> stat_object = vfs_stat . VFSStat ( ) <EOL> stat_object . size = encoded_stream . get_size ( ) <EOL> stat_object . type = stat_object . TYPE_FILE <EOL> finally : <EOL> encoded_stream . close ( ) <EOL> return stat_object </s>
<s> """<STR_LIT>""" <EOL> import pyvshadow <EOL> import dfvfs . vfs . vshadow_file_entry <EOL> from dfvfs import dependencies <EOL> from dfvfs . lib import definitions <EOL> from dfvfs . lib import errors <EOL> from dfvfs . lib import vshadow <EOL> from dfvfs . path import vshadow_path_spec <EOL> from dfvfs . resolver import resolver <EOL> from dfvfs . vfs import file_system <EOL> dependencies . CheckModuleVersion ( u'<STR_LIT>' ) <EOL> class VShadowFileSystem ( file_system . FileSystem ) : <EOL> """<STR_LIT>""" <EOL> LOCATION_ROOT = u'<STR_LIT:/>' <EOL> TYPE_INDICATOR = definitions . TYPE_INDICATOR_VSHADOW <EOL> def __init__ ( self , resolver_context ) : <EOL> """<STR_LIT>""" <EOL> super ( VShadowFileSystem , self ) . __init__ ( resolver_context ) <EOL> self . _file_object = None <EOL> self . _vshadow_volume = None <EOL> def _Close ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _vshadow_volume . close ( ) <EOL> self . _vshadow_volume = None <EOL> self . _file_object . close ( ) <EOL> self . _file_object = None <EOL> def _Open ( self , path_spec , mode = '<STR_LIT:rb>' ) : <EOL> """<STR_LIT>""" <EOL> if not path_spec . HasParent ( ) : <EOL> raise errors . PathSpecError ( <EOL> u'<STR_LIT>' ) <EOL> file_object = resolver . Resolver . OpenFileObject ( <EOL> path_spec . parent , resolver_context = self . _resolver_context ) <EOL> try : <EOL> vshadow_volume = pyvshadow . volume ( ) <EOL> vshadow_volume . open_file_object ( file_object ) <EOL> except : <EOL> file_object . close ( ) <EOL> raise <EOL> self . _file_object = file_object <EOL> self . _vshadow_volume = vshadow_volume <EOL> def FileEntryExistsByPathSpec ( self , path_spec ) : <EOL> """<STR_LIT>""" <EOL> store_index = vshadow . VShadowPathSpecGetStoreIndex ( path_spec ) <EOL> if store_index is None : <EOL> location = getattr ( path_spec , u'<STR_LIT:location>' , None ) <EOL> return location is not None and location == self . LOCATION_ROOT <EOL> return ( store_index >= <NUM_LIT:0> and <EOL> store_index < self . _vshadow_volume . number_of_stores ) <EOL> def GetFileEntryByPathSpec ( self , path_spec ) : <EOL> """<STR_LIT>""" <EOL> store_index = vshadow . VShadowPathSpecGetStoreIndex ( path_spec ) <EOL> if store_index is None : <EOL> location = getattr ( path_spec , u'<STR_LIT:location>' , None ) <EOL> if location is None or location != self . LOCATION_ROOT : <EOL> return <EOL> return dfvfs . vfs . vshadow_file_entry . VShadowFileEntry ( <EOL> self . _resolver_context , self , path_spec , is_root = True , <EOL> is_virtual = True ) <EOL> if store_index < <NUM_LIT:0> or store_index >= self . _vshadow_volume . number_of_stores : <EOL> return <EOL> return dfvfs . vfs . vshadow_file_entry . VShadowFileEntry ( <EOL> self . _resolver_context , self , path_spec ) <EOL> def GetRootFileEntry ( self ) : <EOL> """<STR_LIT>""" <EOL> path_spec = vshadow_path_spec . VShadowPathSpec ( <EOL> location = self . LOCATION_ROOT , parent = self . _path_spec . parent ) <EOL> return self . GetFileEntryByPathSpec ( path_spec ) <EOL> def GetVShadowVolume ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _vshadow_volume </s>
<s> """<STR_LIT>""" <EOL> import unittest <EOL> from dfvfs . path import vmdk_path_spec <EOL> from tests . path import test_lib <EOL> class VMDKPathSpecTest ( test_lib . PathSpecTestCase ) : <EOL> """<STR_LIT>""" <EOL> def testInitialize ( self ) : <EOL> """<STR_LIT>""" <EOL> path_spec = vmdk_path_spec . VMDKPathSpec ( parent = self . _path_spec ) <EOL> self . assertIsNotNone ( path_spec ) <EOL> with self . assertRaises ( ValueError ) : <EOL> _ = vmdk_path_spec . VMDKPathSpec ( parent = None ) <EOL> with self . assertRaises ( ValueError ) : <EOL> _ = vmdk_path_spec . VMDKPathSpec ( parent = self . _path_spec , bogus = u'<STR_LIT>' ) <EOL> def testComparable ( self ) : <EOL> """<STR_LIT>""" <EOL> path_spec = vmdk_path_spec . VMDKPathSpec ( parent = self . _path_spec ) <EOL> self . assertIsNotNone ( path_spec ) <EOL> expected_comparable = u'<STR_LIT:\n>' . join ( [ <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' ] ) <EOL> self . assertEqual ( path_spec . comparable , expected_comparable ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
<s> """<STR_LIT>""" <EOL> import os <EOL> import unittest <EOL> from dfvfs . path import os_path_spec <EOL> from dfvfs . path import qcow_path_spec <EOL> from dfvfs . path import tsk_path_spec <EOL> from dfvfs . path import vshadow_path_spec <EOL> from dfvfs . serializer import protobuf_serializer as serializer <EOL> class ProtobufPathSpecSerializerTest ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> test_file = os . path . join ( u'<STR_LIT>' , u'<STR_LIT>' ) <EOL> self . _os_path_spec = os_path_spec . OSPathSpec ( location = test_file ) <EOL> self . _qcow_path_spec = qcow_path_spec . QCOWPathSpec ( <EOL> parent = self . _os_path_spec ) <EOL> self . _vshadow_path_spec = vshadow_path_spec . VShadowPathSpec ( <EOL> store_index = <NUM_LIT:1> , parent = self . _qcow_path_spec ) <EOL> self . _tsk_path_spec = tsk_path_spec . TSKPathSpec ( <EOL> inode = <NUM_LIT:16> , location = u'<STR_LIT>' , <EOL> parent = self . _vshadow_path_spec ) <EOL> self . _tsk_path_spec_dict = { <EOL> u'<STR_LIT>' : <NUM_LIT:16> , <EOL> u'<STR_LIT:location>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : { <EOL> u'<STR_LIT>' : <NUM_LIT:1> , <EOL> u'<STR_LIT>' : { <EOL> u'<STR_LIT>' : { <EOL> u'<STR_LIT:location>' : os . path . abspath ( test_file ) } <EOL> } <EOL> } <EOL> } <EOL> def testReadAndWriteSerializedObject ( self ) : <EOL> """<STR_LIT>""" <EOL> serialized_path_spec = ( <EOL> serializer . ProtobufPathSpecSerializer . WriteSerializedObject ( <EOL> self . _tsk_path_spec ) ) <EOL> self . assertIsNotNone ( serialized_path_spec ) <EOL> path_spec = serializer . ProtobufPathSpecSerializer . ReadSerializedObject ( <EOL> serialized_path_spec ) <EOL> self . assertIsNotNone ( path_spec ) <EOL> path_spec_dict = path_spec . CopyToDict ( ) <EOL> self . assertEqual ( <EOL> sorted ( path_spec_dict . items ( ) ) , <EOL> sorted ( self . _tsk_path_spec_dict . items ( ) ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
<s> """<STR_LIT>""" <EOL> import os <EOL> import unittest <EOL> from dfvfs . path import os_path_spec <EOL> from dfvfs . path import tsk_partition_path_spec <EOL> from dfvfs . resolver import context <EOL> from dfvfs . vfs import tsk_partition_file_entry <EOL> from dfvfs . vfs import tsk_partition_file_system <EOL> class TSKPartitionFileEntryTest ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _resolver_context = context . Context ( ) <EOL> test_file = os . path . join ( u'<STR_LIT>' , u'<STR_LIT>' ) <EOL> self . _os_path_spec = os_path_spec . OSPathSpec ( location = test_file ) <EOL> self . _tsk_partition_path_spec = ( <EOL> tsk_partition_path_spec . TSKPartitionPathSpec ( <EOL> location = u'<STR_LIT:/>' , parent = self . _os_path_spec ) ) <EOL> self . _file_system = tsk_partition_file_system . TSKPartitionFileSystem ( <EOL> self . _resolver_context ) <EOL> self . _file_system . Open ( self . _tsk_partition_path_spec ) <EOL> def tearDown ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _file_system . Close ( ) <EOL> def testIntialize ( self ) : <EOL> """<STR_LIT>""" <EOL> file_entry = tsk_partition_file_entry . TSKPartitionFileEntry ( <EOL> self . _resolver_context , self . _file_system , <EOL> self . _tsk_partition_path_spec ) <EOL> self . assertIsNotNone ( file_entry ) <EOL> def testGetParentFileEntry ( self ) : <EOL> """<STR_LIT>""" <EOL> path_spec = tsk_partition_path_spec . TSKPartitionPathSpec ( <EOL> part_index = <NUM_LIT:1> , parent = self . _os_path_spec ) <EOL> file_entry = self . _file_system . GetFileEntryByPathSpec ( path_spec ) <EOL> self . assertIsNotNone ( file_entry ) <EOL> parent_file_entry = file_entry . GetParentFileEntry ( ) <EOL> self . assertIsNone ( parent_file_entry ) <EOL> def testGetStat ( self ) : <EOL> """<STR_LIT>""" <EOL> path_spec = tsk_partition_path_spec . TSKPartitionPathSpec ( <EOL> part_index = <NUM_LIT:1> , parent = self . _os_path_spec ) <EOL> file_entry = self . _file_system . GetFileEntryByPathSpec ( path_spec ) <EOL> self . assertIsNotNone ( file_entry ) <EOL> stat_object = file_entry . GetStat ( ) <EOL> self . assertIsNotNone ( stat_object ) <EOL> self . assertEqual ( stat_object . type , stat_object . TYPE_FILE ) <EOL> self . assertEqual ( stat_object . size , <NUM_LIT> ) <EOL> def testIsFunctions ( self ) : <EOL> """<STR_LIT>""" <EOL> path_spec = tsk_partition_path_spec . TSKPartitionPathSpec ( <EOL> part_index = <NUM_LIT:1> , parent = self . _os_path_spec ) <EOL> file_entry = self . _file_system . GetFileEntryByPathSpec ( path_spec ) <EOL> self . assertIsNotNone ( file_entry ) <EOL> self . assertFalse ( file_entry . IsRoot ( ) ) <EOL> self . assertFalse ( file_entry . IsVirtual ( ) ) <EOL> self . assertFalse ( file_entry . IsAllocated ( ) ) <EOL> self . assertFalse ( file_entry . IsDevice ( ) ) <EOL> self . assertFalse ( file_entry . IsDirectory ( ) ) <EOL> self . assertTrue ( file_entry . IsFile ( ) ) <EOL> self . assertFalse ( file_entry . IsLink ( ) ) <EOL> self . assertFalse ( file_entry . IsPipe ( ) ) <EOL> self . assertFalse ( file_entry . IsSocket ( ) ) <EOL> path_spec = tsk_partition_path_spec . TSKPartitionPathSpec ( <EOL> location = u'<STR_LIT:/>' , parent = self . _os_path_spec ) <EOL> file_entry = self . _file_system . GetFileEntryByPathSpec ( path_spec ) <EOL> self . assertIsNotNone ( file_entry ) <EOL> self . assertTrue ( file_entry . IsRoot ( ) ) <EOL> self . assertTrue ( file_entry . IsVirtual ( ) ) <EOL> self . assertTrue ( file_entry . IsAllocated ( ) ) <EOL> self . assertFalse ( file_entry . IsDevice ( ) ) <EOL> self . assertTrue ( file_entry . IsDirectory ( ) ) <EOL> self . assertFalse ( file_entry . IsFile ( ) ) <EOL> self . assertFalse ( file_entry . IsLink ( ) ) <EOL> self . assertFalse ( file_entry . IsPipe ( ) ) <EOL> self . assertFalse ( file_entry . IsSocket ( ) ) <EOL> def testSubFileEntries ( self ) : <EOL> """<STR_LIT>""" <EOL> path_spec = tsk_partition_path_spec . TSKPartitionPathSpec ( <EOL> location = u'<STR_LIT:/>' , parent = self . _os_path_spec ) <EOL> file_entry = self . _file_system . GetFileEntryByPathSpec ( path_spec ) <EOL> self . assertIsNotNone ( file_entry ) <EOL> self . assertEqual ( file_entry . number_of_sub_file_entries , <NUM_LIT:7> ) <EOL> expected_sub_file_entry_names = [ u'<STR_LIT>' , u'<STR_LIT>' , u'<STR_LIT>' , u'<STR_LIT>' , u'<STR_LIT>' , u'<STR_LIT>' , u'<STR_LIT>' ] <EOL> sub_file_entry_names = [ ] <EOL> for sub_file_entry in file_entry . sub_file_entries : <EOL> sub_file_entry_names . append ( sub_file_entry . name ) <EOL> self . assertEqual ( <EOL> len ( sub_file_entry_names ) , len ( expected_sub_file_entry_names ) ) <EOL> self . assertEqual ( <EOL> sorted ( sub_file_entry_names ) , sorted ( expected_sub_file_entry_names ) ) <EOL> def testDataStreams ( self ) : <EOL> """<STR_LIT>""" <EOL> path_spec = tsk_partition_path_spec . TSKPartitionPathSpec ( <EOL> part_index = <NUM_LIT:1> , parent = self . _os_path_spec ) <EOL> file_entry = self . _file_system . GetFileEntryByPathSpec ( path_spec ) <EOL> self . assertIsNotNone ( file_entry ) <EOL> self . assertEqual ( file_entry . number_of_data_streams , <NUM_LIT:1> ) <EOL> data_stream_names = [ ] <EOL> for data_stream in file_entry . data_streams : <EOL> data_stream_names . append ( data_stream . name ) <EOL> self . assertEqual ( data_stream_names , [ u'<STR_LIT>' ] ) <EOL> path_spec = tsk_partition_path_spec . TSKPartitionPathSpec ( <EOL> location = u'<STR_LIT:/>' , parent = self . _os_path_spec ) <EOL> file_entry = self . _file_system . GetFileEntryByPathSpec ( path_spec ) <EOL> self . assertIsNotNone ( file_entry ) <EOL> self . assertEqual ( file_entry . number_of_data_streams , <NUM_LIT:0> ) <EOL> data_stream_names = [ ] <EOL> for data_stream in file_entry . data_streams : <EOL> data_stream_names . append ( data_stream . name ) <EOL> self . assertEqual ( data_stream_names , [ ] ) <EOL> def testGetDataStream ( self ) : <EOL> """<STR_LIT>""" <EOL> path_spec = tsk_partition_path_spec . TSKPartitionPathSpec ( <EOL> part_index = <NUM_LIT:1> , parent = self . _os_path_spec ) <EOL> file_entry = self . _file_system . GetFileEntryByPathSpec ( path_spec ) <EOL> self . assertIsNotNone ( file_entry ) <EOL> data_stream_name = u'<STR_LIT>' <EOL> data_stream = file_entry . GetDataStream ( data_stream_name ) <EOL> self . assertIsNotNone ( data_stream ) <EOL> self . assertEqual ( data_stream . name , data_stream_name ) <EOL> data_stream = file_entry . GetDataStream ( u'<STR_LIT>' ) <EOL> self . assertIsNone ( data_stream ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
<s> from __future__ import print_function <EOL> import os <EOL> from easy_karabiner import alias <EOL> from easy_karabiner import exception <EOL> from easy_karabiner import def_filter_map <EOL> from easy_karabiner . xml_base import XML_base <EOL> class BaseQuery ( object ) : <EOL> DATA_DIR = None <EOL> DATA_SUFFIX = None <EOL> QUERY_ORDER = None <EOL> def __init__ ( self ) : <EOL> self . data = { } <EOL> @ classmethod <EOL> def get_instance ( cls ) : <EOL> if hasattr ( cls , '<STR_LIT>' ) : <EOL> return cls . _instance <EOL> else : <EOL> cls . _instance = cls ( ) <EOL> cls . _instance . load_data ( ) <EOL> return cls . _instance <EOL> @ classmethod <EOL> def query ( cls , value , default = None ) : <EOL> for k in cls . QUERY_ORDER : <EOL> if cls . is_in ( k , value ) : <EOL> return k <EOL> return default <EOL> @ classmethod <EOL> def is_in ( cls , k , value ) : <EOL> if value in cls . get_instance ( ) . get ( k , [ ] ) : <EOL> return k <EOL> else : <EOL> return None <EOL> def get ( self , k , default = None ) : <EOL> return self . data . get ( k , default or [ ] ) <EOL> def load_data ( self ) : <EOL> for type in self . QUERY_ORDER : <EOL> self . data [ type ] = set ( self . get_data ( type ) ) <EOL> return self <EOL> def get_datapath ( self , type ) : <EOL> basename = '<STR_LIT>' % ( type , self . DATA_SUFFIX ) <EOL> return os . path . join ( os . path . dirname ( __file__ ) , self . DATA_DIR , basename ) <EOL> def get_data ( self , type ) : <EOL> raise exception . NeedOverrideError ( ) <EOL> class KeyCodeQuery ( BaseQuery ) : <EOL> '''<STR_LIT>''' <EOL> DATA_DIR = '<STR_LIT>' <EOL> DATA_SUFFIX = '<STR_LIT:data>' <EOL> QUERY_ORDER = [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , ] <EOL> def get_data ( self , type ) : <EOL> data = [ ] <EOL> with open ( self . get_datapath ( type ) , '<STR_LIT:r>' ) as fp : <EOL> lines = fp . readlines ( ) <EOL> need_keep = lambda l : not l . startswith ( '<STR_LIT>' ) and not l . isspace ( ) <EOL> lines = filter ( need_keep , lines ) <EOL> lines = map ( lambda l : l . strip ( ) , lines ) <EOL> data = list ( map ( lambda l : l . split ( ) [ <NUM_LIT:0> ] , lines ) ) <EOL> return data <EOL> class UndefinedFilterException ( Exception ) : <EOL> pass <EOL> class DefQuery ( BaseQuery ) : <EOL> '''<STR_LIT>''' <EOL> DATA_DIR = '<STR_LIT>' <EOL> DATA_SUFFIX = '<STR_LIT>' <EOL> QUERY_ORDER = [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , ] <EOL> def get_data ( self , type ) : <EOL> xml_tree = XML_base . parse ( self . get_datapath ( type ) ) <EOL> name_val = def_filter_map . get_name_tag_by_def_tag ( type ) <EOL> if name_val == '<STR_LIT>' : <EOL> tags = xml_tree . findall ( type ) <EOL> else : <EOL> tags = xml_tree . findall ( '<STR_LIT>' % ( type , name_val ) ) <EOL> return list ( map ( lambda tag : tag . text , tags ) ) <EOL> @ classmethod <EOL> def add_def ( cls , definition ) : <EOL> self = cls . get_instance ( ) <EOL> type = definition . get_def_tag_name ( ) <EOL> defname = definition . get_name ( ) <EOL> self . data [ type ] . add ( defname ) <EOL> @ classmethod <EOL> def query_filter ( cls , def_val ) : <EOL> if alias . get_alias ( '<STR_LIT>' , def_val ) : <EOL> def_type = '<STR_LIT>' <EOL> else : <EOL> def_type = cls . query ( def_val ) <EOL> if def_type is None : <EOL> return def_type <EOL> else : <EOL> return def_filter_map . get_filter_by_def ( def_type ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import doctest <EOL> doctest . testmod ( ) </s>
<s> from logpy . core import ( walk , walkstar , isvar , var , run , <EOL> membero , evalt , fail , success , eq , conde , <EOL> condeseq , goaleval , lany , lall , lanyseq , <EOL> goalexpand , earlyorder , EarlyGoalError , lallearly , earlysafe ) <EOL> import itertools <EOL> from logpy . util import raises <EOL> w , x , y , z = '<STR_LIT>' <EOL> def test_walk ( ) : <EOL> s = { <NUM_LIT:1> : <NUM_LIT:2> , <NUM_LIT:2> : <NUM_LIT:3> } <EOL> assert walk ( <NUM_LIT:2> , s ) == <NUM_LIT:3> <EOL> assert walk ( <NUM_LIT:1> , s ) == <NUM_LIT:3> <EOL> assert walk ( <NUM_LIT:4> , s ) == <NUM_LIT:4> <EOL> def test_deep_walk ( ) : <EOL> """<STR_LIT>""" <EOL> s = { z : <NUM_LIT:6> , y : <NUM_LIT:5> , x : ( y , z ) } <EOL> assert walk ( x , s ) == ( y , z ) <EOL> assert walkstar ( x , s ) == ( <NUM_LIT:5> , <NUM_LIT:6> ) <EOL> def test_eq ( ) : <EOL> x = var ( '<STR_LIT:x>' ) <EOL> assert tuple ( eq ( x , <NUM_LIT:2> ) ( { } ) ) == ( { x : <NUM_LIT:2> } , ) <EOL> assert tuple ( eq ( x , <NUM_LIT:2> ) ( { x : <NUM_LIT:3> } ) ) == ( ) <EOL> def test_lany ( ) : <EOL> x = var ( '<STR_LIT:x>' ) <EOL> assert len ( tuple ( lany ( eq ( x , <NUM_LIT:2> ) , eq ( x , <NUM_LIT:3> ) ) ( { } ) ) ) == <NUM_LIT:2> <EOL> assert len ( tuple ( lany ( ( eq , x , <NUM_LIT:2> ) , ( eq , x , <NUM_LIT:3> ) ) ( { } ) ) ) == <NUM_LIT:2> <EOL> def test_lall ( ) : <EOL> x = var ( '<STR_LIT:x>' ) <EOL> assert results ( lall ( ( eq , x , <NUM_LIT:2> ) ) ) == ( { x : <NUM_LIT:2> } , ) <EOL> assert results ( lall ( ( eq , x , <NUM_LIT:2> ) , ( eq , x , <NUM_LIT:3> ) ) ) == ( ) <EOL> def test_earlysafe ( ) : <EOL> x , y = var ( '<STR_LIT:x>' ) , var ( '<STR_LIT:y>' ) <EOL> assert earlysafe ( ( eq , <NUM_LIT:2> , <NUM_LIT:2> ) ) <EOL> assert earlysafe ( ( eq , <NUM_LIT:2> , <NUM_LIT:3> ) ) <EOL> assert earlysafe ( ( membero , x , ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) ) ) <EOL> assert not earlysafe ( ( membero , x , y ) ) <EOL> def test_earlyorder ( ) : <EOL> x , y = var ( ) , var ( ) <EOL> assert earlyorder ( ( eq , <NUM_LIT:2> , x ) ) == ( ( eq , <NUM_LIT:2> , x ) , ) <EOL> assert earlyorder ( ( eq , <NUM_LIT:2> , x ) , ( eq , <NUM_LIT:3> , x ) ) == ( ( eq , <NUM_LIT:2> , x ) , ( eq , <NUM_LIT:3> , x ) ) <EOL> assert earlyorder ( ( membero , x , y ) , ( eq , y , ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) ) ) [ <NUM_LIT:0> ] == ( eq , y , ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) ) <EOL> def test_conde ( ) : <EOL> x = var ( '<STR_LIT:x>' ) <EOL> assert results ( conde ( [ eq ( x , <NUM_LIT:2> ) ] , [ eq ( x , <NUM_LIT:3> ) ] ) ) == ( { x : <NUM_LIT:2> } , { x : <NUM_LIT:3> } ) <EOL> assert results ( conde ( [ eq ( x , <NUM_LIT:2> ) , eq ( x , <NUM_LIT:3> ) ] ) ) == ( ) <EOL> """<STR_LIT>""" <EOL> def test_short_circuit ( ) : <EOL> def badgoal ( s ) : <EOL> raise NotImplementedError ( ) <EOL> x = var ( '<STR_LIT:x>' ) <EOL> tuple ( run ( <NUM_LIT:5> , x , fail , badgoal ) ) <EOL> def test_run ( ) : <EOL> x , y , z = map ( var , '<STR_LIT>' ) <EOL> assert run ( <NUM_LIT:1> , x , eq ( x , <NUM_LIT:1> ) ) == ( <NUM_LIT:1> , ) <EOL> assert run ( <NUM_LIT:2> , x , eq ( x , <NUM_LIT:1> ) ) == ( <NUM_LIT:1> , ) <EOL> assert run ( <NUM_LIT:0> , x , eq ( x , <NUM_LIT:1> ) ) == ( <NUM_LIT:1> , ) <EOL> assert run ( <NUM_LIT:1> , x , eq ( x , ( y , z ) ) , <EOL> eq ( y , <NUM_LIT:3> ) , <EOL> eq ( z , <NUM_LIT:4> ) ) == ( ( <NUM_LIT:3> , <NUM_LIT:4> ) , ) <EOL> assert set ( run ( <NUM_LIT:2> , x , conde ( [ eq ( x , <NUM_LIT:1> ) ] , [ eq ( x , <NUM_LIT:2> ) ] ) ) ) == set ( ( <NUM_LIT:1> , <NUM_LIT:2> ) ) <EOL> def test_run_output_reify ( ) : <EOL> x = var ( ) <EOL> assert run ( <NUM_LIT:0> , ( <NUM_LIT:1> , <NUM_LIT:2> , x ) , eq ( x , <NUM_LIT:3> ) ) == ( ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) , ) <EOL> def test_membero ( ) : <EOL> x = var ( '<STR_LIT:x>' ) <EOL> assert set ( run ( <NUM_LIT:5> , x , membero ( x , ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) ) , <EOL> membero ( x , ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) ) ) ) == set ( ( <NUM_LIT:2> , <NUM_LIT:3> ) ) <EOL> assert run ( <NUM_LIT:5> , x , membero ( <NUM_LIT:2> , ( <NUM_LIT:1> , x , <NUM_LIT:3> ) ) ) == ( <NUM_LIT:2> , ) <EOL> def test_lanyseq ( ) : <EOL> x = var ( '<STR_LIT:x>' ) <EOL> g = lanyseq ( ( ( eq , x , i ) for i in range ( <NUM_LIT:3> ) ) ) <EOL> assert list ( goaleval ( g ) ( { } ) ) == [ { x : <NUM_LIT:0> } , { x : <NUM_LIT:1> } , { x : <NUM_LIT:2> } ] <EOL> assert list ( goaleval ( g ) ( { } ) ) == [ { x : <NUM_LIT:0> } , { x : <NUM_LIT:1> } , { x : <NUM_LIT:2> } ] <EOL> def test_membero_can_be_reused ( ) : <EOL> x = var ( '<STR_LIT:x>' ) <EOL> g = membero ( x , ( <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ) ) <EOL> assert list ( goaleval ( g ) ( { } ) ) == [ { x : <NUM_LIT:0> } , { x : <NUM_LIT:1> } , { x : <NUM_LIT:2> } ] <EOL> assert list ( goaleval ( g ) ( { } ) ) == [ { x : <NUM_LIT:0> } , { x : <NUM_LIT:1> } , { x : <NUM_LIT:2> } ] <EOL> def test_evalt ( ) : <EOL> add = lambda x , y : x + y <EOL> assert evalt ( ( add , <NUM_LIT:2> , <NUM_LIT:3> ) ) == <NUM_LIT:5> <EOL> assert evalt ( add ( <NUM_LIT:2> , <NUM_LIT:3> ) ) == <NUM_LIT:5> <EOL> assert evalt ( ( <NUM_LIT:1> , <NUM_LIT:2> ) ) == ( <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> def test_uneval_membero ( ) : <EOL> x , y = var ( '<STR_LIT:x>' ) , var ( '<STR_LIT:y>' ) <EOL> assert set ( run ( <NUM_LIT:100> , x , ( membero , y , ( ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) , ( <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ) ) ) , ( membero , x , y ) ) ) == set ( ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ) ) <EOL> def test_goaleval ( ) : <EOL> x , y = var ( '<STR_LIT:x>' ) , var ( '<STR_LIT:y>' ) <EOL> g = eq ( x , <NUM_LIT:2> ) <EOL> assert goaleval ( g ) == g <EOL> assert callable ( goaleval ( ( eq , x , <NUM_LIT:2> ) ) ) <EOL> raises ( EarlyGoalError , lambda : goaleval ( ( membero , x , y ) ) ) <EOL> assert callable ( goaleval ( ( lall , ( eq , x , <NUM_LIT:2> ) ) ) ) <EOL> def test_goalexpand ( ) : <EOL> def growing_goal ( * args ) : <EOL> if len ( args ) < <NUM_LIT:10> : <EOL> return ( growing_goal , <NUM_LIT:1> ) + tuple ( args ) <EOL> else : <EOL> return lambda s : ( <NUM_LIT:1> , ) <EOL> g = ( growing_goal , <NUM_LIT:2> ) <EOL> assert goalexpand ( g ) == ( growing_goal , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> def test_early ( ) : <EOL> x , y = var ( ) , var ( ) <EOL> assert run ( <NUM_LIT:0> , x , lallearly ( ( eq , y , ( <NUM_LIT:1> , <NUM_LIT:2> ) ) , ( membero , x , y ) ) ) <EOL> assert run ( <NUM_LIT:0> , x , lallearly ( ( membero , x , y ) , ( eq , y , ( <NUM_LIT:1> , <NUM_LIT:2> ) ) ) ) <EOL> def test_lany_is_early_safe ( ) : <EOL> x = var ( ) <EOL> y = var ( ) <EOL> assert run ( <NUM_LIT:0> , x , lany ( ( membero , x , y ) , ( eq , x , <NUM_LIT:2> ) ) ) == ( <NUM_LIT:2> , ) <EOL> def results ( g , s = { } ) : <EOL> return tuple ( goaleval ( g ) ( s ) ) <EOL> def test_dict ( ) : <EOL> x = var ( ) <EOL> assert run ( <NUM_LIT:0> , x , eq ( { <NUM_LIT:1> : x } , { <NUM_LIT:1> : <NUM_LIT:2> } ) ) == ( <NUM_LIT:2> , ) </s>
<s> from Mojo . ObjectMapper . ModelPrototype import Model , EmbeddedModelField <EOL> from Mojo . ObjectMapper . Fields import * <EOL> import uuid , datetime <EOL> class Profile ( Model ) : <EOL> """<STR_LIT>""" <EOL> _id = ObjectIDField ( ) <EOL> first_name = StringField ( ) <EOL> last_name = StringField ( ) <EOL> class Group ( Model ) : <EOL> """<STR_LIT>""" <EOL> _id = ObjectIDField ( ) <EOL> group_name = StringField ( allow_empty = False ) <EOL> class User ( Model ) : <EOL> """<STR_LIT>""" <EOL> _id = ObjectIDField ( ) <EOL> username = StringField ( allow_empty = False ) <EOL> password = StringField ( allow_empty = False ) <EOL> email = StringField ( ) <EOL> groups = ListField ( of = Group ) <EOL> active = BooleanField ( default = True ) <EOL> profile = EmbeddedModelField ( to = Profile ) <EOL> is_superuser = BooleanField ( default = False ) <EOL> last_login = DateTimeField ( ) <EOL> date_joined = DateTimeField ( ) <EOL> class Session ( Model ) : <EOL> """<STR_LIT>""" <EOL> _id = ObjectIDField ( ) <EOL> session_key = StringField ( allow_empty = False ) <EOL> session_data = StringField ( ) <EOL> session_expires = DateTimeField ( ) </s>
<s> import optparse <EOL> import os , sys <EOL> import shutil <EOL> import Mojo <EOL> project_files = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> project_dirs = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> app_files = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> app_dirs = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> def make_project_structure ( project_name ) : <EOL> print '<STR_LIT>' % project_name <EOL> mojo_project_dir = os . path . dirname ( Mojo . __file__ ) + '<STR_LIT>' <EOL> new_project_dir = os . getcwd ( ) + '<STR_LIT>' % project_name <EOL> os . mkdir ( new_project_dir ) <EOL> for d in project_dirs : <EOL> os . mkdir ( new_project_dir + '<STR_LIT>' % d ) <EOL> for f in project_files : <EOL> shutil . copyfile ( '<STR_LIT>' . join ( [ mojo_project_dir , f ] ) , '<STR_LIT>' . join ( [ new_project_dir , f ] ) ) <EOL> def make_app_structure ( app_name ) : <EOL> print '<STR_LIT>' % app_name <EOL> mojo_project_dir = os . path . dirname ( Mojo . __file__ ) + '<STR_LIT>' <EOL> first_template = mojo_project_dir + '<STR_LIT>' <EOL> new_app_dir = os . getcwd ( ) + '<STR_LIT>' % app_name <EOL> os . mkdir ( new_app_dir ) <EOL> for d in app_dirs : <EOL> os . mkdir ( new_app_dir + '<STR_LIT>' % d ) <EOL> for f in app_files : <EOL> shutil . copyfile ( '<STR_LIT>' . join ( [ mojo_project_dir , f ] ) , '<STR_LIT>' . join ( [ new_app_dir , f ] ) ) <EOL> first_template_new_dir = new_app_dir + '<STR_LIT>' <EOL> shutil . copyfile ( first_template , first_template_new_dir ) <EOL> def main ( ) : <EOL> p = optparse . OptionParser ( ) <EOL> p . add_option ( '<STR_LIT>' , '<STR_LIT>' , default = '<STR_LIT>' ) <EOL> p . add_option ( '<STR_LIT>' , '<STR_LIT>' , default = '<STR_LIT>' ) <EOL> options , arguments = p . parse_args ( ) <EOL> if options . project : <EOL> make_project_structure ( options . project ) <EOL> if options . app : <EOL> make_app_structure ( options . app ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
<s> import lacore . log <EOL> def setupLogging ( level = <NUM_LIT:0> , logfile = None ) : <EOL> ctx = lacore . log . setupLogging ( level , logfile ) <EOL> getLogger ( ) . addHandler ( <EOL> getLogger ( '<STR_LIT>' ) . handlers [ <NUM_LIT:0> ] ) <EOL> getLogger ( ) . propagate = True <EOL> getLogger ( ) . disabled = False <EOL> return ctx <EOL> def getLogger ( logger = '<STR_LIT>' ) : <EOL> return lacore . log . getLogger ( logger ) </s>
<s> from testtools import TestCase , ExpectedException <EOL> from moto import mock_s3 <EOL> from mock import Mock , patch <EOL> import monoprocessing <EOL> class MPUploadTest ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . s3 = mock_s3 ( ) <EOL> self . s3 . start ( ) <EOL> super ( MPUploadTest , self ) . setUp ( ) <EOL> def tearDown ( self ) : <EOL> self . s3 . stop ( ) <EOL> super ( MPUploadTest , self ) . tearDown ( ) <EOL> def _makeit ( self , * args , ** kw ) : <EOL> from lacli . pool import MPUpload <EOL> return MPUpload ( * args , ** kw ) <EOL> def test_getupload_1 ( self ) : <EOL> mp = self . _makeit ( Mock ( newkey = Mock ( return_value = '<STR_LIT>' ) ) , <EOL> Mock ( chunks = <NUM_LIT:1> ) , '<STR_LIT:foo>' ) <EOL> self . assertEqual ( '<STR_LIT>' , mp . _getupload ( ) ) <EOL> mp . conn . newkey . assert_called_with ( '<STR_LIT:foo>' ) <EOL> def test_getupload_2 ( self ) : <EOL> c = Mock ( ) <EOL> c . newupload . return_value = '<STR_LIT>' <EOL> mp = self . _makeit ( c , Mock ( chunks = <NUM_LIT:2> ) , '<STR_LIT:foo>' ) <EOL> self . assertEqual ( '<STR_LIT>' , mp . _getupload ( ) ) <EOL> c . newupload . assert_called_with ( '<STR_LIT:foo>' ) <EOL> c . getupload . return_value = Mock ( id = '<STR_LIT:bar>' ) <EOL> mp . upload_id = '<STR_LIT:bar>' <EOL> self . assertEqual ( '<STR_LIT:bar>' , mp . _getupload ( ) . id ) <EOL> c . getupload . assert_called ( ) <EOL> def test_submit_job ( self ) : <EOL> source = Mock ( chunks = <NUM_LIT:1> , isfile = True ) <EOL> mp = self . _makeit ( Mock ( ) , source , '<STR_LIT:foo>' ) <EOL> with patch . object ( mp , '<STR_LIT>' ) as do_part : <EOL> do_part . return_value = '<STR_LIT>' <EOL> rs = mp . submit_job ( monoprocessing . Pool ( ) ) <EOL> self . assertEqual ( '<STR_LIT>' , next ( rs ) ) <EOL> with ExpectedException ( StopIteration ) : <EOL> next ( rs ) <EOL> def test_get_result_empty ( self ) : <EOL> rs = Mock ( ) <EOL> rs . next . side_effect = StopIteration <EOL> source = Mock ( chunks = <NUM_LIT:1> , isfile = True ) <EOL> mp = self . _makeit ( Mock ( ) , source , '<STR_LIT:foo>' ) <EOL> from lacore . exceptions import UploadEmptyError <EOL> self . assertRaises ( UploadEmptyError , mp . get_result , rs ) <EOL> def test_get_result_exc_mp ( self ) : <EOL> rs = Mock ( ) <EOL> rs . next . side_effect = [ Mock ( etag = '<STR_LIT:foo>' ) , StopIteration ] <EOL> source = Mock ( chunks = <NUM_LIT:1> , isfile = True ) <EOL> mp = self . _makeit ( Mock ( ) , source , '<STR_LIT:foo>' ) <EOL> from lacli . exceptions import CloudProviderUploadError <EOL> mp . upload = Mock ( ) <EOL> mp . conn . complete_multipart . side_effect = Exception <EOL> with ExpectedException ( CloudProviderUploadError ) : <EOL> mp . get_result ( rs ) <EOL> mp . conn . complete_multipart . assert_called_with ( mp . upload , [ '<STR_LIT:foo>' ] ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_get_result ( self , sp ) : <EOL> rs = Mock ( ) <EOL> rs . next . side_effect = [ Mock ( etag = '<STR_LIT>' ) , StopIteration ] <EOL> source = Mock ( chunks = <NUM_LIT:1> , isfile = True , size = <NUM_LIT> ) <EOL> mp = self . _makeit ( Mock ( ) , source , '<STR_LIT:foo>' ) <EOL> mp . upload = Mock ( ) <EOL> mp . conn . complete_multipart . return_value = Mock ( <EOL> key_name = '<STR_LIT:bar>' , etag = '<STR_LIT>' ) <EOL> etag , source = mp . get_result ( rs ) <EOL> sp . assert_called_with ( '<STR_LIT:bar>' , <NUM_LIT> ) <EOL> mp . conn . complete_multipart . assert_called_with ( mp . upload , [ '<STR_LIT>' ] ) <EOL> self . assertTrue ( source is None ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_get_result2 ( self , cf , mkp , svp ) : <EOL> rs = Mock ( ) <EOL> rs . next . side_effect = [ Mock ( etag = '<STR_LIT>' ) , StopIteration ] <EOL> source = Mock ( chunks = <NUM_LIT:2> , isfile = True , size = <NUM_LIT> ) <EOL> source . chunkstart . return_value = <NUM_LIT> <EOL> cf . return_value = Mock ( size = <NUM_LIT> ) <EOL> mp = self . _makeit ( Mock ( ) , source , '<STR_LIT:foo>' ) <EOL> mp . upload = Mock ( ) <EOL> mp . conn . complete_multipart . return_value = Mock ( <EOL> key_name = '<STR_LIT:bar>' , etag = '<STR_LIT>' ) <EOL> etag , newsource = mp . get_result ( rs ) <EOL> svp . assert_called_with ( '<STR_LIT:bar>' , <NUM_LIT:100> ) <EOL> mkp . assert_called_with ( { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT:0> } ) <EOL> mp . conn . complete_multipart . assert_called_with ( mp . upload , [ '<STR_LIT>' ] ) <EOL> source . chunkstart . assert_called_with ( <NUM_LIT:1> ) <EOL> cf . assert_called_with ( source . path , skip = <NUM_LIT> , chunk = source . chunk ) <EOL> self . assertEqual ( newsource . size , <NUM_LIT> ) </s>
<s> """<STR_LIT>""" <EOL> import numpy as np <EOL> import transforms as dtf <EOL> import buffering as dtb <EOL> import data_utils as dtu <EOL> class InstantiateError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , msg ) : <EOL> self . msg = msg <EOL> def __str__ ( self ) : <EOL> return repr ( self . msg ) <EOL> class BaseGenerator ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , X , y = None , <EOL> aug_params = None , rng_aug_params = None , <EOL> dataset_zmuv = False , dataset_axis = None , <EOL> batch_zmuv = False , batch_axis = None , <EOL> sample_zmuv = False , sample_axis = None ) : <EOL> self . aug_params = aug_params <EOL> self . rng_aug_params = rng_aug_params <EOL> self . dataset_zmuv = dataset_zmuv <EOL> self . dataset_axis = dataset_axis <EOL> self . batch_zmuv = batch_zmuv <EOL> self . batch_axis = batch_axis <EOL> self . sample_zmuv = sample_zmuv <EOL> self . sample_axis = sample_axis <EOL> self . X = np . array ( X ) <EOL> if y is not None : <EOL> self . y = np . array ( y ) <EOL> else : <EOL> self . y = None <EOL> self . mean = None <EOL> self . std = None <EOL> self . tf = None <EOL> self . greyscale = False <EOL> @ property <EOL> def input_shape ( self ) : <EOL> """<STR_LIT>""" <EOL> return np . shape ( self . data_loader ( self . X [ <NUM_LIT:0> ] , ** self . dl_kwargs ) ) <EOL> def resample_dataset ( self , arr , weights , sample_fraction = <NUM_LIT:1.0> , <EOL> rng_seed = None ) : <EOL> """<STR_LIT>""" <EOL> idxs = dtu . resample_data ( arr , weights , sample_fraction = sample_fraction , <EOL> rng_seed = rng_seed ) <EOL> self . X = self . X [ idxs ] <EOL> self . y = self . y [ idxs ] <EOL> print ( "<STR_LIT>" % len ( self . X ) ) <EOL> print ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return idxs <EOL> def set_actions ( self ) : <EOL> """<STR_LIT>""" <EOL> input_shape = self . input_shape [ : <NUM_LIT:2> ] <EOL> if self . dataset_zmuv : <EOL> self . mean , self . std = self . compute_dataset_moments ( ) <EOL> if self . aug_params is not None : <EOL> self . warp_kwargs = self . aug_params . pop ( '<STR_LIT>' , None ) <EOL> self . tf = dtf . build_augmentation_transform ( input_shape , <EOL> ** self . aug_params ) <EOL> self . output_shape = self . aug_params . pop ( '<STR_LIT>' , None ) <EOL> if len ( self . input_shape ) == <NUM_LIT:2> : <EOL> self . greyscale = True <EOL> def standardize ( self , x ) : <EOL> """<STR_LIT>""" <EOL> if ( self . mean is not None ) and ( self . std is not None ) : <EOL> x = x - self . mean <EOL> x = x / ( self . std + <NUM_LIT> ) <EOL> if self . sample_zmuv : <EOL> x = x - np . mean ( x , axis = self . sample_axis ) <EOL> x = x / ( np . std ( x , axis = self . sample_axis ) + <NUM_LIT> ) <EOL> if self . tf is not None : <EOL> x = dtf . transform_image ( x , output_shape = self . output_shape , <EOL> tf = self . tf , warp_kwargs = self . warp_kwargs ) <EOL> if self . rng_aug_params is not None : <EOL> x = dtf . perturb_image ( x , ** self . rng_aug_params ) <EOL> return x <EOL> def get_batch ( self , batch_size = <NUM_LIT:32> , shuffle = False , rng_seed = None , <EOL> buffer_size = <NUM_LIT:2> , dtype = np . float32 , chw_order = False ) : <EOL> """<STR_LIT>""" <EOL> ndata = len ( self . X ) <EOL> if rng_seed is None : <EOL> rng = np . random <EOL> else : <EOL> rng = np . random . RandomState ( seed = rng_seed ) <EOL> idxs = range ( ndata ) <EOL> if shuffle : <EOL> rng . shuffle ( idxs ) <EOL> def gen_batch ( ) : <EOL> nb_batch = int ( np . ceil ( float ( ndata ) / batch_size ) ) <EOL> for b in range ( nb_batch ) : <EOL> batch_end = ( b + <NUM_LIT:1> ) * batch_size <EOL> if batch_end > ndata : <EOL> nb_samples = ndata - b * batch_size <EOL> else : <EOL> nb_samples = batch_size <EOL> bX = [ ] <EOL> for i in xrange ( nb_samples ) : <EOL> idx = idxs [ ( b * batch_size ) + i ] <EOL> x = np . array ( <EOL> self . data_loader ( self . X [ idx ] , ** self . dl_kwargs ) , <EOL> dtype = np . float32 ) <EOL> x = self . standardize ( x ) <EOL> bX . append ( x ) <EOL> bX = np . array ( bX , dtype = dtype ) <EOL> if self . batch_zmuv : <EOL> bX = bX - bX . mean ( axis = self . batch_axis ) <EOL> bX = bX / ( bX . std ( axis = self . batch_axis ) + <NUM_LIT> ) <EOL> if chw_order : <EOL> if self . greyscale : <EOL> bX = np . expand_dims ( bX , <NUM_LIT:3> ) <EOL> bX = bX . transpose ( <NUM_LIT:0> , <NUM_LIT:3> , <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> if self . y is not None : <EOL> bslice = idxs [ b * batch_size : b * batch_size + nb_samples ] <EOL> yield bX , self . y [ bslice ] <EOL> else : <EOL> yield bX <EOL> return dtb . buffered_gen_threaded ( gen_batch ( ) , buffer_size = buffer_size ) <EOL> def compute_dataset_moments ( self ) : <EOL> """<STR_LIT>""" <EOL> raise InstantiateError ( "<STR_LIT>" ) <EOL> def set_data_loader ( self ) : <EOL> """<STR_LIT>""" <EOL> raise InstantiateError ( "<STR_LIT>" ) <EOL> class BatchGenerator ( BaseGenerator ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , X , y = None , <EOL> aug_params = None , rng_aug_params = None , <EOL> dataset_zmuv = False , dataset_axis = None , <EOL> batch_zmuv = False , batch_axis = None , <EOL> sample_zmuv = False , sample_axis = None ) : <EOL> kwargs = locals ( ) <EOL> if '<STR_LIT>' in kwargs : <EOL> kwargs . pop ( '<STR_LIT>' ) <EOL> self . set_data_loader ( ) <EOL> super ( BatchGenerator , self ) . __init__ ( ** kwargs ) <EOL> self . set_actions ( ) <EOL> def compute_dataset_moments ( self ) : <EOL> """<STR_LIT>""" <EOL> mean = self . X . astype ( np . float32 ) . mean ( self . dataset_axis ) <EOL> std = ( self . X . astype ( np . float32 ) - mean ) . std ( self . dataset_axis ) <EOL> return ( mean , std ) <EOL> def set_data_loader ( self ) : <EOL> """<STR_LIT>""" <EOL> def data_loader ( x ) : <EOL> return x <EOL> self . data_loader = data_loader <EOL> self . dl_kwargs = { } <EOL> class DataGenerator ( BaseGenerator ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , X , y = None , data_loader = dtu . img_loader , dl_kwargs = None , <EOL> aug_params = None , rng_aug_params = None , <EOL> dataset_zmuv = False , dataset_axis = None , dataset_zmuv_bsize = <NUM_LIT:32> , <EOL> batch_zmuv = False , batch_axis = None , <EOL> sample_zmuv = False , sample_axis = None ) : <EOL> kwargs = locals ( ) <EOL> if '<STR_LIT>' in kwargs : <EOL> kwargs . pop ( '<STR_LIT>' ) <EOL> self . dataset_zmuv_bsize = kwargs . pop ( '<STR_LIT>' ) <EOL> data_loader = kwargs . pop ( '<STR_LIT>' ) <EOL> dl_kwargs = kwargs . pop ( '<STR_LIT>' ) <EOL> self . set_data_loader ( data_loader , dl_kwargs ) <EOL> super ( DataGenerator , self ) . __init__ ( ** kwargs ) <EOL> self . set_actions ( ) <EOL> def compute_dataset_moments ( self ) : <EOL> """<STR_LIT>""" <EOL> tf = self . tf <EOL> rng_aug_params = self . rng_aug_params <EOL> batch_zmuv = self . batch_zmuv <EOL> sample_zmuv = self . sample_zmuv <EOL> self . tf = None <EOL> self . rng_aug_params = None <EOL> self . batch_zmuv = False <EOL> self . sample_zmuv = False <EOL> batches_mean = [ ] <EOL> batches_std = [ ] <EOL> for ret in self . get_batch ( batch_size = self . dataset_zmuv_bsize ) : <EOL> if self . y is None : <EOL> mb_x = ret <EOL> else : <EOL> mb_x = ret [ <NUM_LIT:0> ] <EOL> mean = mb_x . mean ( axis = self . dataset_axis ) <EOL> std = ( mb_x - mean ) . std ( axis = self . dataset_axis ) <EOL> batches_mean . append ( mean ) <EOL> batches_std . append ( std ) <EOL> self . tf = tf <EOL> self . rng_aug_params = rng_aug_params <EOL> self . batch_zmuv = batch_zmuv <EOL> self . sample_zmuv = sample_zmuv <EOL> return ( np . mean ( batches_mean , axis = <NUM_LIT:0> ) , np . mean ( batches_std , axis = <NUM_LIT:0> ) ) <EOL> def set_data_loader ( self , data_loader , dl_kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . data_loader = data_loader <EOL> if dl_kwargs is None : <EOL> self . dl_kwargs = { } <EOL> else : <EOL> self . dl_kwargs = dl_kwargs </s>
<s> from setuptools import setup <EOL> import choosealicense <EOL> with open ( '<STR_LIT>' ) as f : <EOL> long_description = f . read ( ) <EOL> setup ( <EOL> name = '<STR_LIT>' , <EOL> version = choosealicense . __version__ , <EOL> description = '<STR_LIT>' , <EOL> long_description = long_description , <EOL> url = '<STR_LIT>' , <EOL> author = '<STR_LIT>' , <EOL> author_email = '<STR_LIT>' , <EOL> license = '<STR_LIT>' , <EOL> classifiers = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] , <EOL> keywords = '<STR_LIT>' , <EOL> packages = [ '<STR_LIT>' ] , <EOL> install_requires = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] , <EOL> include_package_data = True , <EOL> entry_points = { <EOL> '<STR_LIT>' : [ <EOL> '<STR_LIT>' ] <EOL> } <EOL> ) </s>
<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import <EOL> import weakref <EOL> import re <EOL> from copy import copy <EOL> import email <EOL> import os <EOL> import json <EOL> import tempfile <EOL> import webbrowser <EOL> import codecs <EOL> from datetime import datetime <EOL> import time <EOL> from lxml . html import HTMLParser <EOL> from lxml . etree import XMLParser , parse , ParserError <EOL> from selection import XpathSelector <EOL> import six <EOL> from six . moves . urllib . parse import urlsplit , parse_qs , urljoin <EOL> import threading <EOL> from six import BytesIO , StringIO <EOL> from weblib . http import smart_urlencode <EOL> import weblib . encoding <EOL> from weblib . files import hashed_path <EOL> from weblib . structured import TreeInterface <EOL> from weblib . text import normalize_space <EOL> from weblib . html import decode_entities , find_refresh_url <EOL> from weblib . rex import normalize_regexp <EOL> import logging <EOL> from grab . cookie import CookieManager <EOL> from grab . error import GrabMisuseError , DataNotFound <EOL> from grab . const import NULL <EOL> from grab . util . warning import warn <EOL> NULL_BYTE = chr ( <NUM_LIT:0> ) <EOL> RE_XML_DECLARATION = re . compile ( br'<STR_LIT>' , re . I ) <EOL> RE_DECLARATION_ENCODING = re . compile ( br'<STR_LIT>' ) <EOL> RE_META_CHARSET = re . compile ( br'<STR_LIT>' , re . I ) <EOL> RE_META_CHARSET_HTML5 = re . compile ( br'<STR_LIT>' , re . I ) <EOL> RE_UNICODE_XML_DECLARATION = re . compile ( RE_XML_DECLARATION . pattern . decode ( '<STR_LIT:utf-8>' ) , re . I ) <EOL> _BOM_TABLE = [ <EOL> ( codecs . BOM_UTF32_BE , '<STR_LIT>' ) , <EOL> ( codecs . BOM_UTF32_LE , '<STR_LIT>' ) , <EOL> ( codecs . BOM_UTF16_BE , '<STR_LIT>' ) , <EOL> ( codecs . BOM_UTF16_LE , '<STR_LIT>' ) , <EOL> ( codecs . BOM_UTF8 , '<STR_LIT:utf-8>' ) <EOL> ] <EOL> _FIRST_CHARS = set ( char [ <NUM_LIT:0> ] for ( char , name ) in _BOM_TABLE ) <EOL> THREAD_STORAGE = threading . local ( ) <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> def read_bom ( data ) : <EOL> """<STR_LIT>""" <EOL> if data and data [ <NUM_LIT:0> ] in _FIRST_CHARS : <EOL> for bom , encoding in _BOM_TABLE : <EOL> if data . startswith ( bom ) : <EOL> return encoding , bom <EOL> return None , None <EOL> class TextExtension ( object ) : <EOL> __slots__ = ( ) <EOL> def text_search ( self , anchor , byte = False ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( anchor , six . text_type ) : <EOL> if byte : <EOL> raise GrabMisuseError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> else : <EOL> return anchor in self . unicode_body ( ) <EOL> if not isinstance ( anchor , six . text_type ) : <EOL> if byte : <EOL> return anchor in self . body <EOL> else : <EOL> raise GrabMisuseError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def text_assert ( self , anchor , byte = False ) : <EOL> """<STR_LIT>""" <EOL> if not self . text_search ( anchor , byte = byte ) : <EOL> raise DataNotFound ( u'<STR_LIT>' % anchor ) <EOL> def text_assert_any ( self , anchors , byte = False ) : <EOL> """<STR_LIT>""" <EOL> found = False <EOL> for anchor in anchors : <EOL> if self . text_search ( anchor , byte = byte ) : <EOL> found = True <EOL> break <EOL> if not found : <EOL> raise DataNotFound ( u'<STR_LIT>' <EOL> % '<STR_LIT:U+002CU+0020>' . join ( anchors ) ) <EOL> class RegexpExtension ( object ) : <EOL> __slots__ = ( ) <EOL> def rex_text ( self , regexp , flags = <NUM_LIT:0> , byte = False , default = NULL ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> match = self . rex_search ( regexp , flags = flags , byte = byte ) <EOL> except DataNotFound : <EOL> if default is NULL : <EOL> raise DataNotFound ( '<STR_LIT>' ) <EOL> else : <EOL> return default <EOL> else : <EOL> return normalize_space ( decode_entities ( match . group ( <NUM_LIT:1> ) ) ) <EOL> def rex_search ( self , regexp , flags = <NUM_LIT:0> , byte = False , default = NULL ) : <EOL> """<STR_LIT>""" <EOL> regexp = normalize_regexp ( regexp , flags ) <EOL> match = None <EOL> if byte : <EOL> if not isinstance ( regexp . pattern , six . text_type ) or not six . PY3 : <EOL> match = regexp . search ( self . body ) <EOL> else : <EOL> if isinstance ( regexp . pattern , six . text_type ) or not six . PY3 : <EOL> ubody = self . unicode_body ( ) <EOL> match = regexp . search ( ubody ) <EOL> if match : <EOL> return match <EOL> else : <EOL> if default is NULL : <EOL> raise DataNotFound ( '<STR_LIT>' % regexp ) <EOL> else : <EOL> return default <EOL> def rex_assert ( self , rex , byte = False ) : <EOL> """<STR_LIT>""" <EOL> self . rex_search ( rex , byte = byte ) <EOL> class PyqueryExtension ( object ) : <EOL> __slots__ = ( ) <EOL> @ property <EOL> def pyquery ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . _pyquery : <EOL> from pyquery import PyQuery <EOL> self . _pyquery = PyQuery ( self . body ) <EOL> return self . _pyquery <EOL> class BodyExtension ( object ) : <EOL> __slots__ = ( ) <EOL> def get_body_chunk ( self ) : <EOL> body_chunk = None <EOL> if self . body_path : <EOL> with open ( self . body_path , '<STR_LIT:rb>' ) as inp : <EOL> body_chunk = inp . read ( <NUM_LIT> ) <EOL> elif self . _bytes_body : <EOL> body_chunk = self . _bytes_body [ : <NUM_LIT> ] <EOL> return body_chunk <EOL> def convert_body_to_unicode ( self , body , bom , charset , <EOL> ignore_errors , fix_special_entities ) : <EOL> if bom : <EOL> body = body [ len ( self . bom ) : ] <EOL> if fix_special_entities : <EOL> body = weblib . encoding . fix_special_entities ( body ) <EOL> if ignore_errors : <EOL> errors = '<STR_LIT:ignore>' <EOL> else : <EOL> errors = '<STR_LIT:strict>' <EOL> return body . decode ( charset , errors ) . strip ( ) <EOL> def read_body_from_file ( self ) : <EOL> with open ( self . body_path , '<STR_LIT:rb>' ) as inp : <EOL> return inp . read ( ) <EOL> def unicode_body ( self , ignore_errors = True , fix_special_entities = True ) : <EOL> """<STR_LIT>""" <EOL> if not self . _unicode_body : <EOL> self . _unicode_body = self . convert_body_to_unicode ( <EOL> body = self . body , <EOL> bom = self . bom , <EOL> charset = self . charset , <EOL> ignore_errors = ignore_errors , <EOL> fix_special_entities = fix_special_entities , <EOL> ) <EOL> return self . _unicode_body <EOL> def _read_body ( self ) : <EOL> if self . body_path : <EOL> return self . read_body_from_file ( ) <EOL> else : <EOL> return self . _bytes_body <EOL> def _write_body ( self , body ) : <EOL> if isinstance ( body , six . text_type ) : <EOL> raise GrabMisuseError ( '<STR_LIT>' ) <EOL> elif self . body_path : <EOL> with open ( self . body_path , '<STR_LIT:wb>' ) as out : <EOL> out . write ( body ) <EOL> self . _bytes_body = None <EOL> else : <EOL> self . _bytes_body = body <EOL> self . _unicode_body = None <EOL> body = property ( _read_body , _write_body ) <EOL> class DomTreeExtension ( object ) : <EOL> __slots__ = ( ) <EOL> @ property <EOL> def tree ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . grab . config [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> return self . build_xml_tree ( ) <EOL> else : <EOL> return self . build_html_tree ( ) <EOL> def _build_dom ( self , content , mode ) : <EOL> assert mode in ( '<STR_LIT:html>' , '<STR_LIT>' ) <EOL> if mode == '<STR_LIT:html>' : <EOL> if not hasattr ( THREAD_STORAGE , '<STR_LIT>' ) : <EOL> THREAD_STORAGE . html_parser = HTMLParser ( ) <EOL> dom = parse ( StringIO ( content ) , <EOL> parser = THREAD_STORAGE . html_parser ) <EOL> return dom . getroot ( ) <EOL> else : <EOL> if not hasattr ( THREAD_STORAGE , '<STR_LIT>' ) : <EOL> THREAD_STORAGE . xml_parser = XMLParser ( ) <EOL> dom = parse ( BytesIO ( content ) , <EOL> parser = THREAD_STORAGE . xml_parser ) <EOL> return dom . getroot ( ) <EOL> def build_html_tree ( self ) : <EOL> from grab . base import GLOBAL_STATE <EOL> if self . _lxml_tree is None : <EOL> fix_setting = self . grab . config [ '<STR_LIT>' ] <EOL> body = self . unicode_body ( fix_special_entities = fix_setting ) . strip ( ) <EOL> if self . grab . config [ '<STR_LIT>' ] : <EOL> body = body . lower ( ) <EOL> if self . grab . config [ '<STR_LIT>' ] : <EOL> body = body . replace ( NULL_BYTE , '<STR_LIT>' ) <EOL> if six . PY3 : <EOL> body = RE_UNICODE_XML_DECLARATION . sub ( '<STR_LIT>' , body ) <EOL> else : <EOL> body = RE_XML_DECLARATION . sub ( '<STR_LIT>' , body ) <EOL> if not body : <EOL> body = '<STR_LIT>' <EOL> start = time . time ( ) <EOL> try : <EOL> self . _lxml_tree = self . _build_dom ( body , '<STR_LIT:html>' ) <EOL> except Exception as ex : <EOL> if ( isinstance ( ex , ParserError ) <EOL> and '<STR_LIT>' in str ( ex ) <EOL> and '<STR_LIT>' not in body ) : <EOL> body = '<STR_LIT>' . format ( body ) <EOL> self . _lxml_tree = self . _build_dom ( body , '<STR_LIT:html>' ) <EOL> elif ( isinstance ( ex , TypeError ) <EOL> and "<STR_LIT>" in str ( ex ) <EOL> and '<STR_LIT>' not in body ) : <EOL> body = '<STR_LIT>' . format ( body ) <EOL> self . _lxml_tree = self . _build_dom ( body , '<STR_LIT:html>' ) <EOL> else : <EOL> raise <EOL> GLOBAL_STATE [ '<STR_LIT>' ] += ( time . time ( ) - start ) <EOL> return self . _lxml_tree <EOL> @ property <EOL> def xml_tree ( self ) : <EOL> """<STR_LIT>""" <EOL> warn ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> return self . build_xml_tree ( ) <EOL> def build_xml_tree ( self ) : <EOL> if self . _strict_lxml_tree is None : <EOL> self . _strict_lxml_tree = self . _build_dom ( self . body , '<STR_LIT>' ) <EOL> return self . _strict_lxml_tree <EOL> class FormExtension ( object ) : <EOL> __slots__ = ( ) <EOL> def choose_form ( self , number = None , id = None , name = None , xpath = None ) : <EOL> """<STR_LIT>""" <EOL> if id is not None : <EOL> try : <EOL> self . _lxml_form = self . select ( '<STR_LIT>' % id ) . node ( ) <EOL> except IndexError : <EOL> raise DataNotFound ( "<STR_LIT>" % id ) <EOL> elif name is not None : <EOL> try : <EOL> self . _lxml_form = self . select ( <EOL> '<STR_LIT>' % name ) . node ( ) <EOL> except IndexError : <EOL> raise DataNotFound ( '<STR_LIT>' % name ) <EOL> elif number is not None : <EOL> try : <EOL> self . _lxml_form = self . tree . forms [ number ] <EOL> except IndexError : <EOL> raise DataNotFound ( '<STR_LIT>' % number ) <EOL> elif xpath is not None : <EOL> try : <EOL> self . _lxml_form = self . select ( xpath ) . node ( ) <EOL> except IndexError : <EOL> raise DataNotFound ( <EOL> '<STR_LIT>' % xpath ) <EOL> else : <EOL> raise GrabMisuseError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> @ property <EOL> def form ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _lxml_form is None : <EOL> forms = [ ( idx , len ( list ( x . fields ) ) ) <EOL> for idx , x in enumerate ( self . tree . forms ) ] <EOL> if len ( forms ) : <EOL> idx = sorted ( forms , key = lambda x : x [ <NUM_LIT:1> ] , reverse = True ) [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> self . choose_form ( idx ) <EOL> else : <EOL> raise DataNotFound ( '<STR_LIT>' ) <EOL> return self . _lxml_form <EOL> def set_input ( self , name , value ) : <EOL> """<STR_LIT>""" <EOL> if self . _lxml_form is None : <EOL> self . choose_form_by_element ( '<STR_LIT>' % name ) <EOL> elem = self . form . inputs [ name ] <EOL> processed = False <EOL> if getattr ( elem , '<STR_LIT:type>' , None ) == '<STR_LIT>' : <EOL> if isinstance ( value , bool ) : <EOL> elem . checked = value <EOL> processed = True <EOL> if not processed : <EOL> if getattr ( elem , '<STR_LIT:type>' , '<STR_LIT>' ) . lower ( ) == '<STR_LIT:file>' : <EOL> self . _file_fields [ name ] = value <EOL> elem . value = '<STR_LIT>' <EOL> else : <EOL> elem . value = value <EOL> def set_input_by_id ( self , _id , value ) : <EOL> """<STR_LIT>""" <EOL> xpath = '<STR_LIT>' % _id <EOL> if self . _lxml_form is None : <EOL> self . choose_form_by_element ( xpath ) <EOL> sel = XpathSelector ( self . form ) <EOL> elem = sel . select ( xpath ) . node ( ) <EOL> return self . set_input ( elem . get ( '<STR_LIT:name>' ) , value ) <EOL> def set_input_by_number ( self , number , value ) : <EOL> """<STR_LIT>""" <EOL> sel = XpathSelector ( self . form ) <EOL> elem = sel . select ( '<STR_LIT>' ) [ number ] . node ( ) <EOL> return self . set_input ( elem . get ( '<STR_LIT:name>' ) , value ) <EOL> def set_input_by_xpath ( self , xpath , value ) : <EOL> """<STR_LIT>""" <EOL> elem = self . select ( xpath ) . node ( ) <EOL> if self . _lxml_form is None : <EOL> parent = elem <EOL> while True : <EOL> parent = parent . getparent ( ) <EOL> if parent . tag == '<STR_LIT>' : <EOL> self . _lxml_form = parent <EOL> break <EOL> return self . set_input ( elem . get ( '<STR_LIT:name>' ) , value ) <EOL> def submit ( self , submit_name = None , make_request = True , <EOL> url = None , extra_post = None ) : <EOL> """<STR_LIT>""" <EOL> post = self . form_fields ( ) <EOL> submit_controls = { } <EOL> for elem in self . form . inputs : <EOL> if ( elem . tag == '<STR_LIT:input>' and elem . type == '<STR_LIT>' and <EOL> elem . get ( '<STR_LIT:name>' ) is not None ) : <EOL> submit_controls [ elem . name ] = elem <EOL> if len ( submit_controls ) : <EOL> if submit_name is None or submit_name not in submit_controls : <EOL> controls = sorted ( submit_controls . values ( ) , <EOL> key = lambda x : x . name ) <EOL> submit_name = controls [ <NUM_LIT:0> ] . name <EOL> for name in submit_controls : <EOL> if name != submit_name : <EOL> if name in post : <EOL> del post [ name ] <EOL> if url : <EOL> action_url = urljoin ( self . url , url ) <EOL> else : <EOL> action_url = urljoin ( self . url , self . form . action ) <EOL> if self . form . method == '<STR_LIT:POST>' : <EOL> if '<STR_LIT>' in self . form . get ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> for key , obj in self . _file_fields . items ( ) : <EOL> post [ key ] = obj <EOL> post_items = list ( post . items ( ) ) <EOL> del post <EOL> if extra_post : <EOL> if isinstance ( extra_post , dict ) : <EOL> extra_post_items = extra_post . items ( ) <EOL> else : <EOL> extra_post_items = extra_post <EOL> keys_to_drop = set ( [ x for x , y in extra_post_items ] ) <EOL> for key in keys_to_drop : <EOL> post_items = [ ( x , y ) for x , y in post_items if x != key ] <EOL> for key , value in extra_post_items : <EOL> post_items . append ( ( key , value ) ) <EOL> if self . form . method == '<STR_LIT:POST>' : <EOL> if '<STR_LIT>' in self . form . get ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> self . grab . setup ( multipart_post = post_items ) <EOL> else : <EOL> self . grab . setup ( post = post_items ) <EOL> self . grab . setup ( url = action_url ) <EOL> else : <EOL> url = action_url . split ( '<STR_LIT:?>' ) [ <NUM_LIT:0> ] + '<STR_LIT:?>' + smart_urlencode ( post_items ) <EOL> self . grab . setup ( url = url ) <EOL> if make_request : <EOL> return self . grab . request ( ) <EOL> else : <EOL> return None <EOL> def form_fields ( self ) : <EOL> """<STR_LIT>""" <EOL> fields = dict ( self . form . fields ) <EOL> for elem in self . form . inputs : <EOL> if not elem . get ( '<STR_LIT:name>' ) : <EOL> continue <EOL> if elem . get ( '<STR_LIT>' ) : <EOL> if elem . name in fields : <EOL> del fields [ elem . name ] <EOL> elif elem . tag == '<STR_LIT>' : <EOL> if fields [ elem . name ] is None : <EOL> if len ( elem . value_options ) : <EOL> fields [ elem . name ] = elem . value_options [ <NUM_LIT:0> ] <EOL> elif getattr ( elem , '<STR_LIT:type>' , None ) == '<STR_LIT>' : <EOL> if fields [ elem . name ] is None : <EOL> fields [ elem . name ] = elem . get ( '<STR_LIT:value>' ) <EOL> elif getattr ( elem , '<STR_LIT:type>' , None ) == '<STR_LIT>' : <EOL> if not elem . checked : <EOL> if elem . name is not None : <EOL> if elem . name in fields : <EOL> del fields [ elem . name ] <EOL> return fields <EOL> def choose_form_by_element ( self , xpath ) : <EOL> elem = self . select ( xpath ) . node ( ) <EOL> while elem is not None : <EOL> if elem . tag == '<STR_LIT>' : <EOL> self . _lxml_form = elem <EOL> return <EOL> else : <EOL> elem = elem . getparent ( ) <EOL> self . _lxml_form = None <EOL> class Document ( TextExtension , RegexpExtension , PyqueryExtension , <EOL> BodyExtension , DomTreeExtension , FormExtension ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = ( '<STR_LIT:status>' , '<STR_LIT:code>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:url>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> ) <EOL> def __init__ ( self , grab = None ) : <EOL> if grab is None : <EOL> self . grab = None <EOL> else : <EOL> if isinstance ( grab , weakref . ProxyType ) : <EOL> self . grab = grab <EOL> else : <EOL> self . grab = weakref . proxy ( grab ) <EOL> self . status = None <EOL> self . code = None <EOL> self . head = None <EOL> self . headers = None <EOL> self . url = None <EOL> self . cookies = CookieManager ( ) <EOL> self . charset = '<STR_LIT:utf-8>' <EOL> self . bom = None <EOL> self . timestamp = datetime . utcnow ( ) <EOL> self . name_lookup_time = <NUM_LIT:0> <EOL> self . connect_time = <NUM_LIT:0> <EOL> self . total_time = <NUM_LIT:0> <EOL> self . download_size = <NUM_LIT:0> <EOL> self . upload_size = <NUM_LIT:0> <EOL> self . download_speed = <NUM_LIT:0> <EOL> self . error_code = None <EOL> self . error_msg = None <EOL> self . from_cache = False <EOL> self . body_path = None <EOL> self . _bytes_body = None <EOL> self . _unicode_body = None <EOL> self . _lxml_tree = None <EOL> self . _strict_lxml_tree = None <EOL> self . _pyquery = None <EOL> self . _lxml_form = None <EOL> self . _file_fields = { } <EOL> def __call__ ( self , query ) : <EOL> return self . select ( query ) <EOL> def select ( self , * args , ** kwargs ) : <EOL> return XpathSelector ( self . tree ) . select ( * args , ** kwargs ) <EOL> def structure ( self , * args , ** kwargs ) : <EOL> return TreeInterface ( self . tree ) . structured_xpath ( * args , ** kwargs ) <EOL> def parse ( self , charset = None , headers = None ) : <EOL> """<STR_LIT>""" <EOL> if headers : <EOL> self . headers = headers <EOL> else : <EOL> if self . head : <EOL> responses = self . head . rsplit ( b'<STR_LIT>' , <NUM_LIT:1> ) <EOL> _ , response = responses [ - <NUM_LIT:1> ] . split ( b'<STR_LIT:\n>' , <NUM_LIT:1> ) <EOL> response = response . decode ( '<STR_LIT:ascii>' , '<STR_LIT:ignore>' ) <EOL> else : <EOL> response = '<STR_LIT>' <EOL> self . headers = email . message_from_string ( response ) <EOL> if charset is None : <EOL> if isinstance ( self . body , six . text_type ) : <EOL> self . charset = '<STR_LIT:utf-8>' <EOL> else : <EOL> self . detect_charset ( ) <EOL> else : <EOL> self . charset = charset . lower ( ) <EOL> self . _unicode_body = None <EOL> def detect_charset ( self ) : <EOL> """<STR_LIT>""" <EOL> charset = None <EOL> body_chunk = self . get_body_chunk ( ) <EOL> if body_chunk : <EOL> match_charset = RE_META_CHARSET . search ( body_chunk ) <EOL> if match_charset : <EOL> charset = match_charset . group ( <NUM_LIT:1> ) <EOL> else : <EOL> match_charset_html5 = RE_META_CHARSET_HTML5 . search ( body_chunk ) <EOL> if match_charset_html5 : <EOL> charset = match_charset_html5 . group ( <NUM_LIT:1> ) <EOL> bom_enc , bom = read_bom ( body_chunk ) <EOL> if bom_enc : <EOL> charset = bom_enc <EOL> self . bom = bom <EOL> if not charset : <EOL> if body_chunk . startswith ( b'<STR_LIT>' ) : <EOL> match = RE_XML_DECLARATION . search ( body_chunk ) <EOL> if match : <EOL> enc_match = RE_DECLARATION_ENCODING . search ( <EOL> match . group ( <NUM_LIT:0> ) ) <EOL> if enc_match : <EOL> charset = enc_match . group ( <NUM_LIT:1> ) <EOL> if not charset : <EOL> if '<STR_LIT:Content-Type>' in self . headers : <EOL> pos = self . headers [ '<STR_LIT:Content-Type>' ] . find ( '<STR_LIT>' ) <EOL> if pos > - <NUM_LIT:1> : <EOL> charset = self . headers [ '<STR_LIT:Content-Type>' ] [ ( pos + <NUM_LIT:8> ) : ] <EOL> if charset : <EOL> charset = charset . lower ( ) <EOL> if not isinstance ( charset , str ) : <EOL> charset = charset . decode ( '<STR_LIT:utf-8>' ) <EOL> try : <EOL> codecs . lookup ( charset ) <EOL> except LookupError : <EOL> logger . error ( '<STR_LIT>' <EOL> '<STR_LIT>' % charset ) <EOL> self . charset = '<STR_LIT:utf-8>' <EOL> else : <EOL> self . charset = charset <EOL> def copy ( self , new_grab = None ) : <EOL> """<STR_LIT>""" <EOL> if new_grab is not None : <EOL> obj = self . __class__ ( self . grab ) <EOL> else : <EOL> obj = self . __class__ ( new_grab ) <EOL> copy_keys = ( '<STR_LIT:status>' , '<STR_LIT:code>' , '<STR_LIT>' , '<STR_LIT:body>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT:url>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> for key in copy_keys : <EOL> setattr ( obj , key , getattr ( self , key ) ) <EOL> obj . headers = copy ( self . headers ) <EOL> obj . cookies = copy ( self . cookies ) <EOL> return obj <EOL> def save ( self , path , create_dirs = False ) : <EOL> """<STR_LIT>""" <EOL> path_dir , path_fname = os . path . split ( path ) <EOL> if not os . path . exists ( path_dir ) : <EOL> try : <EOL> os . makedirs ( path_dir ) <EOL> except OSError : <EOL> pass <EOL> with open ( path , '<STR_LIT:wb>' ) as out : <EOL> out . write ( self . _bytes_body ) <EOL> def save_hash ( self , location , basedir , ext = None ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( location , six . text_type ) : <EOL> location = location . encode ( '<STR_LIT:utf-8>' ) <EOL> rel_path = hashed_path ( location , ext = ext ) <EOL> path = os . path . join ( basedir , rel_path ) <EOL> if not os . path . exists ( path ) : <EOL> path_dir , path_fname = os . path . split ( path ) <EOL> try : <EOL> os . makedirs ( path_dir ) <EOL> except OSError : <EOL> pass <EOL> with open ( path , '<STR_LIT:wb>' ) as out : <EOL> out . write ( self . _bytes_body ) <EOL> return rel_path <EOL> @ property <EOL> def json ( self ) : <EOL> """<STR_LIT>""" <EOL> if six . PY3 : <EOL> return json . loads ( self . body . decode ( self . charset ) ) <EOL> else : <EOL> return json . loads ( self . body ) <EOL> def url_details ( self ) : <EOL> """<STR_LIT>""" <EOL> return urlsplit ( self . url ) <EOL> def query_param ( self , key ) : <EOL> """<STR_LIT>""" <EOL> return parse_qs ( self . url_details ( ) . query ) [ key ] [ <NUM_LIT:0> ] <EOL> def browse ( self ) : <EOL> """<STR_LIT>""" <EOL> fh , path = tempfile . mkstemp ( ) <EOL> self . save ( path ) <EOL> webbrowser . open ( '<STR_LIT>' + path ) <EOL> @ property <EOL> def time ( self ) : <EOL> warn ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> return self . total_time <EOL> def __getstate__ ( self ) : <EOL> """<STR_LIT>""" <EOL> state = { } <EOL> for cls in type ( self ) . mro ( ) : <EOL> cls_slots = getattr ( cls , '<STR_LIT>' , ( ) ) <EOL> for slot in cls_slots : <EOL> if slot != '<STR_LIT>' : <EOL> if hasattr ( self , slot ) : <EOL> state [ slot ] = getattr ( self , slot ) <EOL> state [ '<STR_LIT>' ] = None <EOL> state [ '<STR_LIT>' ] = None <EOL> state [ '<STR_LIT>' ] = None <EOL> return state <EOL> def __setstate__ ( self , state ) : <EOL> for slot , value in state . items ( ) : <EOL> setattr ( self , slot , value ) <EOL> def get_meta_refresh_url ( self ) : <EOL> return find_refresh_url ( self . unicode_body ( ) ) </s>
<s> from importlib import import_module <EOL> import logging <EOL> import sys <EOL> from grab . util . warning import warn <EOL> class CustomImporter ( object ) : <EOL> """<STR_LIT>""" <EOL> virtual_name = '<STR_LIT>' <EOL> def find_module ( self , name , path = None ) : <EOL> """<STR_LIT>""" <EOL> if name . find ( self . virtual_name ) == <NUM_LIT:0> : <EOL> name = name . split ( '<STR_LIT:.>' ) <EOL> if name [ - <NUM_LIT:1> ] == '<STR_LIT>' : <EOL> name [ - <NUM_LIT:1> ] = '<STR_LIT>' <EOL> if len ( name ) == <NUM_LIT:3> : <EOL> self . name = '<STR_LIT:.>' + '<STR_LIT:.>' . join ( name [ <NUM_LIT:2> : ] ) <EOL> else : <EOL> self . name = '<STR_LIT>' <EOL> return self <EOL> return None <EOL> def load_module ( self , name ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> module = import_module ( self . name , '<STR_LIT>' ) <EOL> sys . modules [ name ] = module <EOL> warn ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( self . name , self . name ) ) <EOL> except : <EOL> raise ImportError ( name ) <EOL> return module </s>
<s> import grab . null . zz </s>
<s> from unittest import TestCase <EOL> import unittest <EOL> import re <EOL> import six <EOL> from user_agent import ( generate_user_agent , generate_navigator , <EOL> generate_navigator_js , <EOL> UserAgentRuntimeError , UserAgentInvalidRequirements ) <EOL> class UserAgentTestCase ( TestCase ) : <EOL> def test_it ( self ) : <EOL> ua = generate_user_agent ( ) <EOL> self . assertTrue ( len ( ua ) > <NUM_LIT:0> ) <EOL> def test_platform_option ( self ) : <EOL> for x in range ( <NUM_LIT:100> ) : <EOL> ua = generate_user_agent ( platform = '<STR_LIT>' ) <EOL> self . assertTrue ( '<STR_LIT>' in ua . lower ( ) ) <EOL> ua = generate_user_agent ( platform = '<STR_LIT>' ) <EOL> self . assertTrue ( '<STR_LIT>' in ua . lower ( ) ) <EOL> ua = generate_user_agent ( platform = '<STR_LIT>' ) <EOL> self . assertTrue ( '<STR_LIT>' in ua . lower ( ) ) <EOL> self . assertRaises ( UserAgentRuntimeError , <EOL> generate_user_agent , <EOL> platform = <NUM_LIT:11> ) <EOL> def test_navigator_option ( self ) : <EOL> for x in range ( <NUM_LIT:100> ) : <EOL> ua = generate_user_agent ( navigator = '<STR_LIT>' ) <EOL> self . assertTrue ( '<STR_LIT>' in ua . lower ( ) ) <EOL> ua = generate_user_agent ( navigator = '<STR_LIT>' ) <EOL> self . assertTrue ( '<STR_LIT>' in ua . lower ( ) ) <EOL> ua = generate_user_agent ( navigator = '<STR_LIT>' ) <EOL> self . assertTrue ( '<STR_LIT>' in ua . lower ( ) or '<STR_LIT>' in ua . lower ( ) ) <EOL> def test_navigator_option_tuple ( self ) : <EOL> for x in range ( <NUM_LIT:100> ) : <EOL> ua = generate_user_agent ( navigator = ( '<STR_LIT>' , ) ) <EOL> ua = generate_user_agent ( navigator = ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> ua = generate_user_agent ( navigator = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> def test_platform_option_tuple ( self ) : <EOL> for x in range ( <NUM_LIT:100> ) : <EOL> ua = generate_user_agent ( platform = ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> ua = generate_user_agent ( platform = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> ua = generate_user_agent ( platform = ( '<STR_LIT>' , ) ) <EOL> ua = generate_user_agent ( platform = ( '<STR_LIT>' , ) ) <EOL> ua = generate_user_agent ( platform = ( '<STR_LIT>' , ) ) <EOL> def test_platform_navigator_option ( self ) : <EOL> for x in range ( <NUM_LIT:100> ) : <EOL> ua = generate_user_agent ( platform = '<STR_LIT>' , navigator = '<STR_LIT>' ) <EOL> self . assertTrue ( '<STR_LIT>' in ua . lower ( ) ) <EOL> self . assertTrue ( '<STR_LIT>' in ua . lower ( ) ) <EOL> ua = generate_user_agent ( platform = '<STR_LIT>' , navigator = '<STR_LIT>' ) <EOL> self . assertTrue ( '<STR_LIT>' in ua . lower ( ) ) <EOL> self . assertTrue ( '<STR_LIT>' in ua . lower ( ) ) <EOL> ua = generate_user_agent ( platform = '<STR_LIT>' , navigator = '<STR_LIT>' ) <EOL> self . assertTrue ( '<STR_LIT>' in ua . lower ( ) or '<STR_LIT>' in ua . lower ( ) ) <EOL> self . assertTrue ( '<STR_LIT>' in ua . lower ( ) ) <EOL> def test_platform_linux ( self ) : <EOL> for x in range ( <NUM_LIT:100> ) : <EOL> ua = generate_user_agent ( platform = '<STR_LIT>' ) <EOL> self . assertTrue ( ua . startswith ( '<STR_LIT>' ) ) <EOL> def test_mac_chrome ( self ) : <EOL> for x in range ( <NUM_LIT:100> ) : <EOL> ua = generate_user_agent ( platform = '<STR_LIT>' , navigator = '<STR_LIT>' ) <EOL> self . assertTrue ( re . search ( r'<STR_LIT>' , ua ) ) <EOL> def test_impossible_combination ( self ) : <EOL> for x in range ( <NUM_LIT:100> ) : <EOL> self . assertRaises ( UserAgentInvalidRequirements , <EOL> generate_user_agent , <EOL> platform = '<STR_LIT>' , navigator = '<STR_LIT>' ) <EOL> self . assertRaises ( UserAgentInvalidRequirements , <EOL> generate_user_agent , <EOL> platform = '<STR_LIT>' , navigator = '<STR_LIT>' ) <EOL> def test_generate_navigator_js ( self ) : <EOL> for x in range ( <NUM_LIT:100> ) : <EOL> nav = generate_navigator_js ( ) <EOL> self . assertEqual ( set ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] ) , set ( nav . keys ( ) ) ) <EOL> self . assertEqual ( nav [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertTrue ( nav [ '<STR_LIT>' ] in ( <EOL> '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> def test_data_integrity ( self ) : <EOL> for x in range ( <NUM_LIT:100> ) : <EOL> nav = generate_navigator ( ) <EOL> for key , val in nav . items ( ) : <EOL> self . assertTrue ( isinstance ( val , six . string_types ) ) <EOL> def test_platform_value ( self ) : <EOL> for x in range ( <NUM_LIT:100> ) : <EOL> nav = generate_navigator ( platform = '<STR_LIT>' ) <EOL> self . assertTrue ( '<STR_LIT>' in nav [ '<STR_LIT>' ] ) <EOL> nav = generate_navigator ( platform = '<STR_LIT>' ) <EOL> self . assertTrue ( '<STR_LIT>' in nav [ '<STR_LIT>' ] ) <EOL> nav = generate_navigator ( platform = '<STR_LIT>' ) <EOL> self . assertTrue ( '<STR_LIT>' in nav [ '<STR_LIT>' ] ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
<s> """<STR_LIT>""" <EOL> __docformat__ = '<STR_LIT>' <EOL> __version__ = '<STR_LIT>' <EOL> from ctypes import * <EOL> from . import dll <EOL> SDL_GetTicks = dll . function ( <EOL> '<STR_LIT>' , <EOL> '''<STR_LIT>''' , <EOL> args = [ ] , <EOL> arg_types = [ ] , <EOL> return_type = c_uint ) <EOL> SDL_Delay = dll . function ( <EOL> '<STR_LIT>' , <EOL> '''<STR_LIT>''' , <EOL> args = [ '<STR_LIT>' ] , <EOL> arg_types = [ c_uint ] , <EOL> return_type = None ) <EOL> _SDL_TimerCallback = CFUNCTYPE ( c_int , c_uint ) <EOL> _SDL_SetTimer = dll . private_function ( <EOL> '<STR_LIT>' , <EOL> arg_types = [ c_uint , _SDL_TimerCallback ] , <EOL> return_type = c_int ) <EOL> _timercallback_ref = None <EOL> def SDL_SetTimer ( interval , callback ) : <EOL> """<STR_LIT>""" <EOL> global _timercallback_ref <EOL> if callback : <EOL> _timercallback_ref = _SDL_TimerCallback ( callback ) <EOL> else : <EOL> _timercallback_ref = _SDL_TimerCallback ( ) <EOL> if _SDL_SetTimer ( interval , _timercallback_ref ) == - <NUM_LIT:1> : <EOL> raise SDL . error . SDL_Exception ( SDL . error . SDL_GetError ( ) ) <EOL> _SDL_NewTimerCallback = CFUNCTYPE ( c_uint , c_int , c_void_p ) <EOL> _SDL_AddTimer = dll . private_function ( <EOL> '<STR_LIT>' , <EOL> arg_types = [ c_uint , _SDL_NewTimerCallback , c_void_p ] , <EOL> return_type = c_void_p ) <EOL> _timer_refs = { } <EOL> def SDL_AddTimer ( interval , callback , param ) : <EOL> """<STR_LIT>""" <EOL> def _callback ( interval , _ignored_param ) : <EOL> return callback ( interval , param ) <EOL> func = _SDL_NewTimerCallback ( _callback ) <EOL> result = _SDL_AddTimer ( interval , func , None ) <EOL> if not result : <EOL> raise SDL . error . SDL_Exception ( SDL . error . SDL_GetError ( ) ) <EOL> _timer_refs [ result ] = func <EOL> return result <EOL> _SDL_RemoveTimer = dll . private_function ( <EOL> '<STR_LIT>' , <EOL> args = [ '<STR_LIT:t>' ] , <EOL> arg_types = [ c_void_p ] , <EOL> return_type = c_int , <EOL> error_return = <NUM_LIT:0> ) <EOL> def SDL_RemoveTimer ( t ) : <EOL> """<STR_LIT>""" <EOL> global _timer_refs <EOL> _SDL_RemoveTimer ( t ) <EOL> del _timer_refs [ t ] </s>
<s> """<STR_LIT>""" <EOL> from __future__ import division , print_function , unicode_literals <EOL> __docformat__ = '<STR_LIT>' <EOL> from cocos . director import director <EOL> from . base_layers import Layer <EOL> import pyglet <EOL> from pyglet import gl <EOL> class ScrollableLayer ( Layer ) : <EOL> """<STR_LIT>""" <EOL> view_x , view_y = <NUM_LIT:0> , <NUM_LIT:0> <EOL> view_w , view_h = <NUM_LIT:0> , <NUM_LIT:0> <EOL> origin_x = origin_y = origin_z = <NUM_LIT:0> <EOL> def __init__ ( self , parallax = <NUM_LIT:1> ) : <EOL> super ( ScrollableLayer , self ) . __init__ ( ) <EOL> self . parallax = parallax <EOL> self . transform_anchor_x = <NUM_LIT:0> <EOL> self . transform_anchor_y = <NUM_LIT:0> <EOL> self . batch = pyglet . graphics . Batch ( ) <EOL> def on_enter ( self ) : <EOL> director . push_handlers ( self . on_cocos_resize ) <EOL> super ( ScrollableLayer , self ) . on_enter ( ) <EOL> def on_exit ( self ) : <EOL> super ( ScrollableLayer , self ) . on_exit ( ) <EOL> director . pop_handlers ( ) <EOL> def set_view ( self , x , y , w , h , viewport_ox = <NUM_LIT:0> , viewport_oy = <NUM_LIT:0> ) : <EOL> x *= self . parallax <EOL> y *= self . parallax <EOL> self . view_x , self . view_y = x , y <EOL> self . view_w , self . view_h = w , h <EOL> x -= self . origin_x <EOL> y -= self . origin_y <EOL> x -= viewport_ox <EOL> y -= viewport_oy <EOL> self . position = ( - x , - y ) <EOL> def draw ( self ) : <EOL> super ( ScrollableLayer , self ) . draw ( ) <EOL> gl . glPushMatrix ( ) <EOL> self . transform ( ) <EOL> self . batch . draw ( ) <EOL> gl . glPopMatrix ( ) <EOL> def set_dirty ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def on_cocos_resize ( self , usable_width , usable_height ) : <EOL> self . set_dirty ( ) <EOL> class ScrollingManager ( Layer ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , viewport = None , do_not_scale = None ) : <EOL> if do_not_scale is None : <EOL> do_not_scale = not director . autoscale <EOL> self . autoscale = not do_not_scale and director . autoscale <EOL> self . viewport = viewport <EOL> self . view_x , self . view_y = <NUM_LIT:0> , <NUM_LIT:0> <EOL> self . view_w , self . view_h = <NUM_LIT:1> , <NUM_LIT:1> <EOL> self . childs_ox = <NUM_LIT:0> <EOL> self . childs_oy = <NUM_LIT:0> <EOL> self . fx = self . fy = <NUM_LIT:0> <EOL> super ( ScrollingManager , self ) . __init__ ( ) <EOL> self . transform_anchor_x = <NUM_LIT:0> <EOL> self . transform_anchor_y = <NUM_LIT:0> <EOL> def on_enter ( self ) : <EOL> super ( ScrollingManager , self ) . on_enter ( ) <EOL> director . push_handlers ( self . on_cocos_resize ) <EOL> self . update_view_size ( ) <EOL> self . refresh_focus ( ) <EOL> def on_exit ( self ) : <EOL> director . pop_handlers ( ) <EOL> super ( ScrollingManager , self ) . on_exit ( ) <EOL> def update_view_size ( self ) : <EOL> if self . viewport is not None : <EOL> self . view_w , self . view_h = self . viewport . width , self . viewport . height <EOL> self . view_x , self . view_y = getattr ( self . viewport , '<STR_LIT>' , ( <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> if not director . autoscale : <EOL> self . _scissor_flat = ( self . view_x , self . view_y , <EOL> self . view_w , self . view_h ) <EOL> else : <EOL> w , h = director . get_window_size ( ) <EOL> sx = director . _usable_width / w <EOL> sy = director . _usable_height / h <EOL> self . _scissor_flat = ( int ( self . view_x * sx ) , int ( self . view_y * sy ) , <EOL> int ( self . view_w * sx ) , int ( self . view_h * sy ) ) <EOL> elif self . autoscale : <EOL> self . view_w , self . view_h = director . get_window_size ( ) <EOL> else : <EOL> self . view_w = director . _usable_width <EOL> self . view_h = director . _usable_height <EOL> def on_cocos_resize ( self , usable_width , usable_height ) : <EOL> self . update_view_size ( ) <EOL> self . refresh_focus ( ) <EOL> def refresh_focus ( self ) : <EOL> if self . children : <EOL> self . _old_focus = None <EOL> self . set_focus ( self . fx , self . fy ) <EOL> _scale = <NUM_LIT:1.0> <EOL> def set_scale ( self , scale ) : <EOL> self . _scale = <NUM_LIT:1.0> * scale <EOL> self . refresh_focus ( ) <EOL> scale = property ( lambda s : s . _scale , set_scale ) <EOL> def add ( self , child , z = <NUM_LIT:0> , name = None ) : <EOL> """<STR_LIT>""" <EOL> super ( ScrollingManager , self ) . add ( child , z = z , name = name ) <EOL> self . set_focus ( self . fx , self . fy , force = True ) <EOL> def pixel_from_screen ( self , x , y ) : <EOL> """<STR_LIT>""" <EOL> if director . autoscale : <EOL> x , y = director . get_virtual_coordinates ( x , y ) <EOL> ww , wh = director . get_window_size ( ) <EOL> sx = x / self . view_w <EOL> sy = y / self . view_h <EOL> vx , vy = self . childs_ox , self . childs_oy <EOL> w = int ( self . view_w / self . scale ) <EOL> h = int ( self . view_h / self . scale ) <EOL> return int ( vx + sx * w ) , int ( vy + sy * h ) <EOL> def pixel_to_screen ( self , x , y ) : <EOL> """<STR_LIT>""" <EOL> screen_x = self . scale * ( x - self . childs_ox ) <EOL> screen_y = self . scale * ( y - self . childs_oy ) <EOL> return int ( screen_x ) , int ( screen_y ) <EOL> _old_focus = None <EOL> def set_focus ( self , fx , fy , force = False ) : <EOL> """<STR_LIT>""" <EOL> if not [ l for z , l in self . children if hasattr ( l , '<STR_LIT>' ) ] : <EOL> return self . force_focus ( fx , fy ) <EOL> self . fx , self . fy = fx , fy <EOL> a = ( fx , fy , self . scale ) <EOL> if not force and self . _old_focus == a : <EOL> return <EOL> self . _old_focus = a <EOL> x1 = [ ] <EOL> y1 = [ ] <EOL> x2 = [ ] <EOL> y2 = [ ] <EOL> for z , layer in self . children : <EOL> if not hasattr ( layer , '<STR_LIT>' ) : <EOL> continue <EOL> x1 . append ( layer . origin_x ) <EOL> y1 . append ( layer . origin_y ) <EOL> x2 . append ( layer . origin_x + layer . px_width ) <EOL> y2 . append ( layer . origin_y + layer . px_height ) <EOL> b_min_x = min ( x1 ) <EOL> b_min_y = min ( y1 ) <EOL> b_max_x = max ( x2 ) <EOL> b_max_y = max ( y2 ) <EOL> w = self . view_w / self . scale <EOL> h = self . view_h / self . scale <EOL> w2 , h2 = w / <NUM_LIT:2> , h / <NUM_LIT:2> <EOL> if ( b_max_x - b_min_x ) <= w : <EOL> restricted_fx = ( b_max_x + b_min_x ) / <NUM_LIT:2> <EOL> else : <EOL> if ( fx - w2 ) < b_min_x : <EOL> restricted_fx = b_min_x + w2 <EOL> elif ( fx + w2 ) > b_max_x : <EOL> restricted_fx = b_max_x - w2 <EOL> else : <EOL> restricted_fx = fx <EOL> if ( b_max_y - b_min_y ) <= h : <EOL> restricted_fy = ( b_max_y + b_min_y ) / <NUM_LIT:2> <EOL> else : <EOL> if ( fy - h2 ) < b_min_y : <EOL> restricted_fy = b_min_y + h2 <EOL> elif ( fy + h2 ) > b_max_y : <EOL> restricted_fy = b_max_y - h2 <EOL> else : <EOL> restricted_fy = fy <EOL> self . restricted_fx = restricted_fx <EOL> self . restricted_fy = restricted_fy <EOL> x , y = ( restricted_fx - w2 ) , ( restricted_fy - h2 ) <EOL> childs_scroll_x = x <EOL> childs_scroll_y = y <EOL> self . childs_ox = childs_scroll_x - self . view_x / self . scale <EOL> self . childs_oy = childs_scroll_y - self . view_y / self . scale <EOL> for z , layer in self . children : <EOL> layer . set_view ( childs_scroll_x , childs_scroll_y , w , h , <EOL> self . view_x / self . scale , self . view_y / self . scale ) <EOL> def force_focus ( self , fx , fy ) : <EOL> """<STR_LIT>""" <EOL> self . fx , self . fy = map ( int , ( fx , fy ) ) <EOL> self . fx , self . fy = fx , fy <EOL> w = int ( self . view_w / self . scale ) <EOL> h = int ( self . view_h / self . scale ) <EOL> w2 , h2 = w // <NUM_LIT:2> , h // <NUM_LIT:2> <EOL> x , y = fx - w2 , fy - h2 <EOL> childs_scroll_x = x <EOL> childs_scroll_y = y <EOL> self . childs_ox = childs_scroll_x - self . view_x / self . scale <EOL> self . childs_oy = childs_scroll_y - self . view_y / self . scale <EOL> for z , layer in self . children : <EOL> layer . set_view ( childs_scroll_x , childs_scroll_y , w , h , <EOL> self . view_x / self . scale , self . view_y / self . scale ) <EOL> def set_state ( self ) : <EOL> self . _scissor_enabled = gl . glIsEnabled ( gl . GL_SCISSOR_TEST ) <EOL> self . _old_scissor_flat = ( gl . GLint * <NUM_LIT:4> ) ( ) <EOL> gl . glGetIntegerv ( gl . GL_SCISSOR_BOX , self . _old_scissor_flat ) <EOL> if not self . _scissor_enabled : <EOL> gl . glEnable ( gl . GL_SCISSOR_TEST ) <EOL> gl . glScissor ( * self . _scissor_flat ) <EOL> def unset_state ( self ) : <EOL> gl . glScissor ( * self . _old_scissor_flat ) <EOL> if not self . _scissor_enabled : <EOL> gl . glDisable ( gl . GL_SCISSOR_TEST ) <EOL> def visit ( self ) : <EOL> if self . viewport is not None : <EOL> self . set_state ( ) <EOL> super ( ScrollingManager , self ) . visit ( ) <EOL> self . unset_state ( ) <EOL> else : <EOL> super ( ScrollingManager , self ) . visit ( ) </s>
<s> from __future__ import division , print_function , unicode_literals <EOL> import sys <EOL> import os <EOL> sys . path . insert ( <NUM_LIT:0> , os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' ) ) <EOL> import random <EOL> import math <EOL> import pyglet <EOL> from pyglet . window import key <EOL> from pyglet . gl import * <EOL> import cocos <EOL> from cocos . director import director <EOL> import cocos . collision_model as cm <EOL> import cocos . euclid as eu <EOL> import cocos . actions as ac <EOL> fe = <NUM_LIT> <EOL> consts = { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:width>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : True , <EOL> "<STR_LIT>" : True <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:width>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : { <EOL> key . LEFT : '<STR_LIT:left>' , <EOL> key . RIGHT : '<STR_LIT:right>' , <EOL> key . UP : '<STR_LIT>' , <EOL> } <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : '<STR_LIT>' , <EOL> "<STR_LIT>" : { <EOL> '<STR_LIT>' : ( <NUM_LIT:0> , <NUM_LIT> , <NUM_LIT> ) , <EOL> '<STR_LIT>' : ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) , <EOL> '<STR_LIT>' : ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) , <EOL> '<STR_LIT>' : ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) , <EOL> '<STR_LIT>' : ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) <EOL> } <EOL> } <EOL> } <EOL> scale_x = consts [ "<STR_LIT>" ] [ "<STR_LIT:width>" ] / consts [ "<STR_LIT>" ] [ "<STR_LIT:width>" ] <EOL> scale_y = consts [ "<STR_LIT>" ] [ "<STR_LIT>" ] / consts [ "<STR_LIT>" ] [ "<STR_LIT>" ] <EOL> def world_to_view ( v ) : <EOL> """<STR_LIT>""" <EOL> return v . x * scale_x , v . y * scale_y <EOL> class Actor ( cocos . sprite . Sprite ) : <EOL> palette = { } <EOL> def __init__ ( self , cx , cy , radius , btype , img , vel = None ) : <EOL> super ( Actor , self ) . __init__ ( img ) <EOL> self . scale = ( radius * <NUM_LIT> ) * scale_x / ( self . image . width / <NUM_LIT> ) <EOL> self . btype = btype <EOL> self . color = self . palette [ btype ] <EOL> self . cshape = cm . CircleShape ( eu . Vector2 ( cx , cy ) , radius ) <EOL> self . update_center ( self . cshape . center ) <EOL> if vel is None : <EOL> vel = eu . Vector2 ( <NUM_LIT:0.0> , <NUM_LIT:0.0> ) <EOL> self . vel = vel <EOL> def update_center ( self , cshape_center ) : <EOL> """<STR_LIT>""" <EOL> self . position = world_to_view ( cshape_center ) <EOL> self . cshape . center = cshape_center <EOL> class MessageLayer ( cocos . layer . Layer ) : <EOL> """<STR_LIT>""" <EOL> def show_message ( self , msg , callback = None ) : <EOL> w , h = director . get_window_size ( ) <EOL> self . msg = cocos . text . Label ( msg , <EOL> font_size = <NUM_LIT> , <EOL> font_name = consts [ '<STR_LIT>' ] [ '<STR_LIT>' ] , <EOL> anchor_y = '<STR_LIT>' , <EOL> anchor_x = '<STR_LIT>' ) <EOL> self . msg . position = ( w / <NUM_LIT> , h ) <EOL> self . add ( self . msg ) <EOL> actions = ( <EOL> ac . Show ( ) + ac . Accelerate ( ac . MoveBy ( ( <NUM_LIT:0> , - h / <NUM_LIT> ) , duration = <NUM_LIT:0.5> ) ) + <EOL> ac . Delay ( <NUM_LIT:1> ) + <EOL> ac . Accelerate ( ac . MoveBy ( ( <NUM_LIT:0> , - h / <NUM_LIT> ) , duration = <NUM_LIT:0.5> ) ) + <EOL> ac . Hide ( ) <EOL> ) <EOL> if callback : <EOL> actions += ac . CallFunc ( callback ) <EOL> self . msg . do ( actions ) <EOL> def reflection_y ( a ) : <EOL> assert isinstance ( a , eu . Vector2 ) <EOL> return eu . Vector2 ( a . x , - a . y ) <EOL> class Worldview ( cocos . layer . Layer ) : <EOL> """<STR_LIT>""" <EOL> is_event_handler = True <EOL> def __init__ ( self , fn_show_message = None ) : <EOL> super ( Worldview , self ) . __init__ ( ) <EOL> self . fn_show_message = fn_show_message <EOL> world = consts [ '<STR_LIT>' ] <EOL> self . width = world [ '<STR_LIT:width>' ] <EOL> self . height = world [ '<STR_LIT>' ] <EOL> self . rPlayer = world [ '<STR_LIT>' ] <EOL> self . wall_scale_min = world [ '<STR_LIT>' ] <EOL> self . wall_scale_max = world [ '<STR_LIT>' ] <EOL> self . topSpeed = world [ '<STR_LIT>' ] <EOL> self . angular_velocity = world [ '<STR_LIT>' ] <EOL> self . accel = world [ '<STR_LIT>' ] <EOL> pics = { } <EOL> pics [ "<STR_LIT>" ] = pyglet . resource . image ( '<STR_LIT>' ) <EOL> pics [ "<STR_LIT>" ] = pyglet . resource . image ( '<STR_LIT>' ) <EOL> pics [ "<STR_LIT>" ] = pyglet . resource . image ( '<STR_LIT>' ) <EOL> self . pics = pics <EOL> cell_size = self . rPlayer * self . wall_scale_max * <NUM_LIT> * <NUM_LIT> <EOL> self . collman = cm . CollisionManagerGrid ( <NUM_LIT:0.0> , self . width , <EOL> <NUM_LIT:0.0> , self . height , <EOL> cell_size , cell_size ) <EOL> self . bindings = world [ '<STR_LIT>' ] <EOL> buttons = { } <EOL> for k in self . bindings : <EOL> buttons [ self . bindings [ k ] ] = <NUM_LIT:0> <EOL> self . buttons = buttons <EOL> self . toRemove = set ( ) <EOL> self . schedule ( self . update ) <EOL> self . ladder_begin ( ) <EOL> def ladder_begin ( self ) : <EOL> self . level_num = <NUM_LIT:0> <EOL> self . empty_level ( ) <EOL> msg = '<STR_LIT>' <EOL> self . fn_show_message ( msg , callback = self . level_launch ) <EOL> def level_launch ( self ) : <EOL> self . generate_random_level ( ) <EOL> msg = '<STR_LIT>' % self . level_num <EOL> self . fn_show_message ( msg , callback = self . level_start ) <EOL> def level_start ( self ) : <EOL> self . win_status = '<STR_LIT>' <EOL> def level_conquered ( self ) : <EOL> self . win_status = '<STR_LIT>' <EOL> msg = '<STR_LIT>' % self . level_num <EOL> self . fn_show_message ( msg , callback = self . level_next ) <EOL> def level_losed ( self ) : <EOL> self . win_status = '<STR_LIT>' <EOL> msg = '<STR_LIT>' <EOL> self . fn_show_message ( msg , callback = self . ladder_begin ) <EOL> def level_next ( self ) : <EOL> self . empty_level ( ) <EOL> self . level_num += <NUM_LIT:1> <EOL> self . level_launch ( ) <EOL> def empty_level ( self ) : <EOL> for node in self . get_children ( ) : <EOL> self . remove ( node ) <EOL> assert len ( self . children ) == <NUM_LIT:0> <EOL> self . player = None <EOL> self . gate = None <EOL> self . food_cnt = <NUM_LIT:0> <EOL> self . toRemove . clear ( ) <EOL> self . win_status = '<STR_LIT>' <EOL> self . topSpeed = <NUM_LIT> <EOL> self . impulse_dir = eu . Vector2 ( <NUM_LIT:0.0> , <NUM_LIT:1.0> ) <EOL> self . impulseForce = <NUM_LIT:0.0> <EOL> def generate_random_level ( self ) : <EOL> food_num = <NUM_LIT:5> <EOL> food_scale = <NUM_LIT:1.0> <EOL> wall_num = <NUM_LIT:10> <EOL> gate_scale = <NUM_LIT> <EOL> min_separation_rel = <NUM_LIT> <EOL> width = self . width <EOL> height = self . height <EOL> rPlayer = self . rPlayer <EOL> min_separation = min_separation_rel * rPlayer <EOL> wall_scale_min = self . wall_scale_min <EOL> wall_scale_max = self . wall_scale_max <EOL> pics = self . pics <EOL> z = <NUM_LIT:0> <EOL> cx , cy = ( <NUM_LIT:0.5> * width , <NUM_LIT:0.5> * height ) <EOL> self . player = Actor ( cx , cy , rPlayer , '<STR_LIT>' , pics [ '<STR_LIT>' ] ) <EOL> self . collman . add ( self . player ) <EOL> minSeparation = min_separation * <NUM_LIT> * rPlayer <EOL> rGate = gate_scale * rPlayer <EOL> self . gate = Actor ( cx , cy , rGate , '<STR_LIT>' , pics [ '<STR_LIT>' ] ) <EOL> self . gate . color = Actor . palette [ '<STR_LIT>' ] <EOL> cntTrys = <NUM_LIT:0> <EOL> while cntTrys < <NUM_LIT:100> : <EOL> cx = rGate + random . random ( ) * ( width - <NUM_LIT> * rGate ) <EOL> cy = rGate + random . random ( ) * ( height - <NUM_LIT> * rGate ) <EOL> self . gate . update_center ( eu . Vector2 ( cx , cy ) ) <EOL> if not self . collman . they_collide ( self . player , self . gate ) : <EOL> break <EOL> cntTrys += <NUM_LIT:1> <EOL> self . add ( self . gate , z = z ) <EOL> z += <NUM_LIT:1> <EOL> self . collman . add ( self . gate ) <EOL> rFood = food_scale * rPlayer <EOL> self . cnt_food = <NUM_LIT:0> <EOL> for i in range ( food_num ) : <EOL> food = Actor ( cx , cy , rFood , '<STR_LIT>' , pics [ '<STR_LIT>' ] ) <EOL> cntTrys = <NUM_LIT:0> <EOL> while cntTrys < <NUM_LIT:100> : <EOL> cx = rFood + random . random ( ) * ( width - <NUM_LIT> * rFood ) <EOL> cy = rFood + random . random ( ) * ( height - <NUM_LIT> * rFood ) <EOL> food . update_center ( eu . Vector2 ( cx , cy ) ) <EOL> if self . collman . any_near ( food , min_separation ) is None : <EOL> self . cnt_food += <NUM_LIT:1> <EOL> self . add ( food , z = z ) <EOL> z += <NUM_LIT:1> <EOL> self . collman . add ( food ) <EOL> break <EOL> cntTrys += <NUM_LIT:1> <EOL> for i in range ( wall_num ) : <EOL> s = random . random ( ) <EOL> r = rPlayer * ( wall_scale_min * s + wall_scale_max * ( <NUM_LIT:1.0> - s ) ) <EOL> wall = Actor ( cx , cy , r , '<STR_LIT>' , pics [ '<STR_LIT>' ] ) <EOL> cntTrys = <NUM_LIT:0> <EOL> while cntTrys < <NUM_LIT:100> : <EOL> cx = r + random . random ( ) * ( width - <NUM_LIT> * r ) <EOL> cy = r + random . random ( ) * ( height - <NUM_LIT> * r ) <EOL> wall . update_center ( eu . Vector2 ( cx , cy ) ) <EOL> if self . collman . any_near ( wall , min_separation ) is None : <EOL> self . add ( wall , z = z ) <EOL> z += <NUM_LIT:1> <EOL> self . collman . add ( wall ) <EOL> break <EOL> cntTrys += <NUM_LIT:1> <EOL> self . add ( self . player , z = z ) <EOL> z += <NUM_LIT:1> <EOL> def update ( self , dt ) : <EOL> if self . win_status != '<STR_LIT>' : <EOL> return <EOL> self . collman . clear ( ) <EOL> for z , node in self . children : <EOL> self . collman . add ( node ) <EOL> for other in self . collman . iter_colliding ( self . player ) : <EOL> typeball = other . btype <EOL> if typeball == '<STR_LIT>' : <EOL> self . toRemove . add ( other ) <EOL> self . cnt_food -= <NUM_LIT:1> <EOL> if not self . cnt_food : <EOL> self . open_gate ( ) <EOL> elif ( typeball == '<STR_LIT>' or <EOL> typeball == '<STR_LIT>' and self . cnt_food > <NUM_LIT:0> ) : <EOL> self . level_losed ( ) <EOL> elif typeball == '<STR_LIT>' : <EOL> self . level_conquered ( ) <EOL> buttons = self . buttons <EOL> ma = buttons [ '<STR_LIT:right>' ] - buttons [ '<STR_LIT:left>' ] <EOL> if ma != <NUM_LIT:0> : <EOL> self . player . rotation += ma * dt * self . angular_velocity <EOL> a = math . radians ( self . player . rotation ) <EOL> self . impulse_dir = eu . Vector2 ( math . sin ( a ) , math . cos ( a ) ) <EOL> newVel = self . player . vel <EOL> mv = buttons [ '<STR_LIT>' ] <EOL> if mv != <NUM_LIT:0> : <EOL> newVel += dt * mv * self . accel * self . impulse_dir <EOL> nv = newVel . magnitude ( ) <EOL> if nv > self . topSpeed : <EOL> newVel *= self . topSpeed / nv <EOL> ppos = self . player . cshape . center <EOL> newPos = ppos <EOL> r = self . player . cshape . r <EOL> while dt > <NUM_LIT> : <EOL> newPos = ppos + dt * newVel <EOL> consumed_dt = dt <EOL> if newPos . x < r : <EOL> consumed_dt = ( r - ppos . x ) / newVel . x <EOL> newPos = ppos + consumed_dt * newVel <EOL> newVel = - reflection_y ( newVel ) <EOL> if newPos . x > ( self . width - r ) : <EOL> consumed_dt = ( self . width - r - ppos . x ) / newVel . x <EOL> newPos = ppos + consumed_dt * newVel <EOL> newVel = - reflection_y ( newVel ) <EOL> if newPos . y < r : <EOL> consumed_dt = ( r - ppos . y ) / newVel . y <EOL> newPos = ppos + consumed_dt * newVel <EOL> newVel = reflection_y ( newVel ) <EOL> if newPos . y > ( self . height - r ) : <EOL> consumed_dt = ( self . height - r - ppos . y ) / newVel . y <EOL> newPos = ppos + consumed_dt * newVel <EOL> newVel = reflection_y ( newVel ) <EOL> dt -= consumed_dt <EOL> self . player . vel = newVel <EOL> self . player . update_center ( newPos ) <EOL> for node in self . toRemove : <EOL> self . remove ( node ) <EOL> self . toRemove . clear ( ) <EOL> def open_gate ( self ) : <EOL> self . gate . color = Actor . palette [ '<STR_LIT>' ] <EOL> def on_key_press ( self , k , m ) : <EOL> binds = self . bindings <EOL> if k in binds : <EOL> self . buttons [ binds [ k ] ] = <NUM_LIT:1> <EOL> return True <EOL> return False <EOL> def on_key_release ( self , k , m ) : <EOL> binds = self . bindings <EOL> if k in binds : <EOL> self . buttons [ binds [ k ] ] = <NUM_LIT:0> <EOL> return True <EOL> return False <EOL> def main ( ) : <EOL> director . init ( ** consts [ '<STR_LIT>' ] ) <EOL> scene = cocos . scene . Scene ( ) <EOL> palette = consts [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> Actor . palette = palette <EOL> r , g , b = palette [ '<STR_LIT>' ] <EOL> scene . add ( cocos . layer . ColorLayer ( r , g , b , <NUM_LIT:255> ) , z = - <NUM_LIT:1> ) <EOL> message_layer = MessageLayer ( ) <EOL> scene . add ( message_layer , z = <NUM_LIT:1> ) <EOL> playview = Worldview ( fn_show_message = message_layer . show_message ) <EOL> scene . add ( playview , z = <NUM_LIT:0> ) <EOL> director . run ( scene ) <EOL> main ( ) </s>
<s> from __future__ import division , print_function , unicode_literals <EOL> import sys <EOL> import os <EOL> sys . path . insert ( <NUM_LIT:0> , os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' ) ) <EOL> from cocos . director import director <EOL> from cocos . layer import * <EOL> from cocos . scene import Scene <EOL> from cocos . scenes . transitions import * <EOL> from cocos . actions import * <EOL> from cocos . sprite import * <EOL> from cocos . menu import * <EOL> from cocos . text import * <EOL> import pyglet <EOL> from pyglet import gl , font <EOL> from pyglet . window import key <EOL> from HUD import BackgroundLayer <EOL> import soundex <EOL> import hiscore <EOL> class ScoresLayer ( ColorLayer ) : <EOL> FONT_SIZE = <NUM_LIT:30> <EOL> is_event_handler = True <EOL> def __init__ ( self ) : <EOL> w , h = director . get_window_size ( ) <EOL> super ( ScoresLayer , self ) . __init__ ( <NUM_LIT:32> , <NUM_LIT:32> , <NUM_LIT:32> , <NUM_LIT:16> , width = w , height = h - <NUM_LIT> ) <EOL> self . font_title = { } <EOL> self . font_title [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . font_title [ '<STR_LIT>' ] = <NUM_LIT> <EOL> self . font_title [ '<STR_LIT>' ] = ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:255> ) <EOL> self . font_title [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . font_title [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> title = Label ( '<STR_LIT>' , ** self . font_title ) <EOL> title . position = ( w / <NUM_LIT> , h ) <EOL> self . add ( title , z = <NUM_LIT:1> ) <EOL> self . table = None <EOL> def on_enter ( self ) : <EOL> super ( ScoresLayer , self ) . on_enter ( ) <EOL> scores = hiscore . hiscore . get ( ) <EOL> if self . table : <EOL> self . remove_old ( ) <EOL> self . table = [ ] <EOL> for idx , s in enumerate ( scores ) : <EOL> pos = Label ( '<STR_LIT>' % ( idx + <NUM_LIT:1> ) , font_name = '<STR_LIT>' , <EOL> font_size = self . FONT_SIZE , <EOL> anchor_y = '<STR_LIT>' , <EOL> anchor_x = '<STR_LIT:left>' , <EOL> color = ( <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> ) ) <EOL> name = Label ( s [ <NUM_LIT:1> ] , font_name = '<STR_LIT>' , <EOL> font_size = self . FONT_SIZE , <EOL> anchor_y = '<STR_LIT>' , <EOL> anchor_x = '<STR_LIT:left>' , <EOL> color = ( <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> ) ) <EOL> score = Label ( str ( s [ <NUM_LIT:0> ] ) , font_name = '<STR_LIT>' , <EOL> font_size = self . FONT_SIZE , <EOL> anchor_y = '<STR_LIT>' , <EOL> anchor_x = '<STR_LIT:right>' , <EOL> color = ( <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> ) ) <EOL> lvl = Label ( str ( s [ <NUM_LIT:2> ] ) , font_name = '<STR_LIT>' , <EOL> font_size = self . FONT_SIZE , <EOL> anchor_y = '<STR_LIT>' , <EOL> anchor_x = '<STR_LIT:right>' , <EOL> color = ( <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> ) ) <EOL> self . table . append ( ( pos , name , score , lvl ) ) <EOL> self . process_table ( ) <EOL> def remove_old ( self ) : <EOL> for item in self . table : <EOL> pos , name , score , lvl = item <EOL> self . remove ( pos ) <EOL> self . remove ( name ) <EOL> self . remove ( score ) <EOL> self . remove ( lvl ) <EOL> self . table = None <EOL> def process_table ( self ) : <EOL> w , h = director . get_window_size ( ) <EOL> for idx , item in enumerate ( self . table ) : <EOL> pos , name , score , lvl = item <EOL> posy = h - <NUM_LIT:100> - ( ( self . FONT_SIZE + <NUM_LIT:15> ) * idx ) <EOL> pos . position = ( <NUM_LIT:5> , posy ) <EOL> name . position = ( <NUM_LIT> , posy ) <EOL> score . position = ( w - <NUM_LIT> , posy ) <EOL> lvl . position = ( w - <NUM_LIT:10> , posy ) <EOL> self . add ( pos , z = <NUM_LIT:2> ) <EOL> self . add ( name , z = <NUM_LIT:2> ) <EOL> self . add ( score , z = <NUM_LIT:2> ) <EOL> self . add ( lvl , z = <NUM_LIT:2> ) <EOL> def on_key_press ( self , k , m ) : <EOL> if k in ( key . ENTER , key . ESCAPE , key . SPACE ) : <EOL> self . parent . switch_to ( <NUM_LIT:0> ) <EOL> return True <EOL> def on_mouse_release ( self , x , y , b , m ) : <EOL> self . parent . switch_to ( <NUM_LIT:0> ) <EOL> return True <EOL> class OptionsMenu ( Menu ) : <EOL> def __init__ ( self ) : <EOL> super ( OptionsMenu , self ) . __init__ ( '<STR_LIT>' ) <EOL> self . select_sound = soundex . load ( '<STR_LIT>' ) <EOL> self . font_title [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . font_title [ '<STR_LIT>' ] = <NUM_LIT> <EOL> self . font_title [ '<STR_LIT>' ] = ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:255> ) <EOL> self . font_item [ '<STR_LIT>' ] = '<STR_LIT>' , <EOL> self . font_item [ '<STR_LIT>' ] = ( <NUM_LIT:32> , <NUM_LIT:16> , <NUM_LIT:32> , <NUM_LIT:255> ) <EOL> self . font_item [ '<STR_LIT>' ] = <NUM_LIT:32> <EOL> self . font_item_selected [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . font_item_selected [ '<STR_LIT>' ] = ( <NUM_LIT:32> , <NUM_LIT:16> , <NUM_LIT:32> , <NUM_LIT:255> ) <EOL> self . font_item_selected [ '<STR_LIT>' ] = <NUM_LIT> <EOL> self . menu_anchor_y = CENTER <EOL> self . menu_anchor_x = CENTER <EOL> items = [ ] <EOL> self . volumes = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:100>' ] <EOL> items . append ( MultipleMenuItem ( <EOL> '<STR_LIT>' , <EOL> self . on_sfx_volume , <EOL> self . volumes , <EOL> int ( soundex . sound_vol * <NUM_LIT:10> ) ) <EOL> ) <EOL> items . append ( MultipleMenuItem ( <EOL> '<STR_LIT>' , <EOL> self . on_music_volume , <EOL> self . volumes , <EOL> int ( soundex . music_player . volume * <NUM_LIT:10> ) ) <EOL> ) <EOL> items . append ( ToggleMenuItem ( '<STR_LIT>' , self . on_show_fps , director . show_FPS ) ) <EOL> items . append ( MenuItem ( '<STR_LIT>' , self . on_fullscreen ) ) <EOL> items . append ( MenuItem ( '<STR_LIT>' , self . on_quit ) ) <EOL> self . create_menu ( items , shake ( ) , shake_back ( ) ) <EOL> def on_fullscreen ( self ) : <EOL> director . window . set_fullscreen ( not director . window . fullscreen ) <EOL> def on_quit ( self ) : <EOL> self . parent . switch_to ( <NUM_LIT:0> ) <EOL> def on_show_fps ( self , value ) : <EOL> director . show_FPS = value <EOL> def on_sfx_volume ( self , idx ) : <EOL> vol = idx / <NUM_LIT> <EOL> soundex . sound_volume ( vol ) <EOL> def on_music_volume ( self , idx ) : <EOL> vol = idx / <NUM_LIT> <EOL> soundex . music_volume ( vol ) <EOL> class MainMenu ( Menu ) : <EOL> def __init__ ( self ) : <EOL> super ( MainMenu , self ) . __init__ ( '<STR_LIT>' ) <EOL> self . select_sound = soundex . load ( '<STR_LIT>' ) <EOL> self . font_title [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . font_title [ '<STR_LIT>' ] = <NUM_LIT> <EOL> self . font_title [ '<STR_LIT>' ] = ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:255> ) <EOL> self . font_item [ '<STR_LIT>' ] = '<STR_LIT>' , <EOL> self . font_item [ '<STR_LIT>' ] = ( <NUM_LIT:32> , <NUM_LIT:16> , <NUM_LIT:32> , <NUM_LIT:255> ) <EOL> self . font_item [ '<STR_LIT>' ] = <NUM_LIT:32> <EOL> self . font_item_selected [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . font_item_selected [ '<STR_LIT>' ] = ( <NUM_LIT:32> , <NUM_LIT:16> , <NUM_LIT:32> , <NUM_LIT:255> ) <EOL> self . font_item_selected [ '<STR_LIT>' ] = <NUM_LIT> <EOL> self . menu_anchor_y = CENTER <EOL> self . menu_anchor_x = CENTER <EOL> items = [ ] <EOL> items . append ( MenuItem ( '<STR_LIT>' , self . on_new_game ) ) <EOL> items . append ( MenuItem ( '<STR_LIT>' , self . on_options ) ) <EOL> items . append ( MenuItem ( '<STR_LIT>' , self . on_scores ) ) <EOL> items . append ( MenuItem ( '<STR_LIT>' , self . on_quit ) ) <EOL> self . create_menu ( items , shake ( ) , shake_back ( ) ) <EOL> def on_new_game ( self ) : <EOL> import gameview <EOL> director . push ( FlipAngular3DTransition ( <EOL> gameview . get_newgame ( ) , <NUM_LIT> ) ) <EOL> def on_options ( self ) : <EOL> self . parent . switch_to ( <NUM_LIT:1> ) <EOL> def on_scores ( self ) : <EOL> self . parent . switch_to ( <NUM_LIT:2> ) <EOL> def on_quit ( self ) : <EOL> pyglet . app . exit ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> pyglet . resource . path . append ( '<STR_LIT:data>' ) <EOL> pyglet . resource . reindex ( ) <EOL> font . add_directory ( '<STR_LIT:data>' ) <EOL> director . init ( resizable = True , width = <NUM_LIT> , height = <NUM_LIT> ) <EOL> scene = Scene ( ) <EOL> scene . add ( MultiplexLayer ( <EOL> MainMenu ( ) , <EOL> OptionsMenu ( ) , <EOL> ScoresLayer ( ) , <EOL> ) , <EOL> z = <NUM_LIT:1> ) <EOL> scene . add ( BackgroundLayer ( ) , z = <NUM_LIT:0> ) <EOL> director . run ( scene ) </s>
<s> from __future__ import division , print_function , unicode_literals <EOL> import sys <EOL> import os <EOL> sys . path . insert ( <NUM_LIT:0> , os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT:..>' ) ) <EOL> testinfo = "<STR_LIT>" <EOL> tags = "<STR_LIT>" <EOL> import cocos <EOL> from cocos . director import director <EOL> from cocos . actions import CallFuncS , Show , Delay <EOL> from cocos . sprite import Sprite <EOL> import pyglet <EOL> class TestLayer ( cocos . layer . Layer ) : <EOL> def __init__ ( self ) : <EOL> super ( TestLayer , self ) . __init__ ( ) <EOL> x , y = director . get_window_size ( ) <EOL> self . sprite = Sprite ( '<STR_LIT>' , ( x // <NUM_LIT:2> , y // <NUM_LIT:2> ) ) <EOL> self . sprite . visible = False <EOL> self . add ( self . sprite ) <EOL> def make_visible ( sp ) : <EOL> sp . do ( Show ( ) ) <EOL> self . sprite . do ( Delay ( <NUM_LIT:1> ) + CallFuncS ( make_visible ) ) <EOL> description = """<STR_LIT>""" <EOL> def main ( ) : <EOL> print ( description ) <EOL> director . init ( ) <EOL> test_layer = TestLayer ( ) <EOL> main_scene = cocos . scene . Scene ( test_layer ) <EOL> director . run ( main_scene ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
<s> from __future__ import division , print_function , unicode_literals <EOL> import sys <EOL> import os <EOL> sys . path . insert ( <NUM_LIT:0> , os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT:..>' ) ) <EOL> testinfo = "<STR_LIT>" <EOL> tags = "<STR_LIT>" <EOL> import cocos <EOL> from cocos . director import director <EOL> from cocos . actions import FadeTo <EOL> from cocos . sprite import Sprite <EOL> import pyglet <EOL> class TestLayer ( cocos . layer . Layer ) : <EOL> def __init__ ( self ) : <EOL> super ( TestLayer , self ) . __init__ ( ) <EOL> x , y = director . get_window_size ( ) <EOL> self . sprite = Sprite ( '<STR_LIT>' , ( x // <NUM_LIT:2> , y // <NUM_LIT:2> ) ) <EOL> self . add ( self . sprite ) <EOL> self . sprite . do ( FadeTo ( <NUM_LIT:0> , <NUM_LIT:5> ) ) <EOL> self . sprite2 = Sprite ( '<STR_LIT>' , ( x // <NUM_LIT:4> * <NUM_LIT:3> , y // <NUM_LIT:2> ) , opacity = <NUM_LIT:0> ) <EOL> self . add ( self . sprite2 ) <EOL> self . sprite2 . do ( FadeTo ( <NUM_LIT:255> , <NUM_LIT:5> ) ) <EOL> def main ( ) : <EOL> director . init ( ) <EOL> test_layer = TestLayer ( ) <EOL> main_scene = cocos . scene . Scene ( test_layer ) <EOL> director . run ( main_scene ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
<s> from __future__ import division , print_function , unicode_literals <EOL> import sys <EOL> import os <EOL> sys . path . insert ( <NUM_LIT:0> , os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT:..>' ) ) <EOL> testinfo = "<STR_LIT>" <EOL> tags = "<STR_LIT>" <EOL> from pyglet import image <EOL> from pyglet . gl import * <EOL> from pyglet import font <EOL> from cocos . director import * <EOL> from cocos . menu import * <EOL> from cocos . scene import * <EOL> from cocos . layer import * <EOL> class MainMenu ( Menu ) : <EOL> def __init__ ( self ) : <EOL> super ( MainMenu , self ) . __init__ ( "<STR_LIT>" ) <EOL> self . menu_valign = BOTTOM <EOL> self . menu_halign = RIGHT <EOL> items = [ <EOL> ( MenuItem ( '<STR_LIT>' , self . on_quit ) ) , <EOL> ( MenuItem ( '<STR_LIT>' , self . on_quit ) ) , <EOL> ( MenuItem ( '<STR_LIT>' , self . on_quit ) ) , <EOL> ( MenuItem ( '<STR_LIT>' , self . on_quit ) ) , <EOL> ] <EOL> self . create_menu ( items , selected_effect = zoom_in ( ) , <EOL> unselected_effect = zoom_out ( ) ) <EOL> def on_quit ( self ) : <EOL> pyglet . app . exit ( ) <EOL> def main ( ) : <EOL> pyglet . font . add_directory ( '<STR_LIT:.>' ) <EOL> director . init ( resizable = True ) <EOL> director . run ( Scene ( MainMenu ( ) ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
<s> from __future__ import division , print_function , unicode_literals <EOL> import sys <EOL> import os <EOL> sys . path . insert ( <NUM_LIT:0> , os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT:..>' ) ) <EOL> testinfo = "<STR_LIT>" <EOL> tags = "<STR_LIT>" <EOL> import cocos <EOL> from cocos . director import director <EOL> from cocos . sprite import Sprite <EOL> from cocos . actions import * <EOL> import pyglet <EOL> class TestLayer ( cocos . layer . Layer ) : <EOL> def __init__ ( self ) : <EOL> super ( TestLayer , self ) . __init__ ( ) <EOL> x , y = director . get_window_size ( ) <EOL> self . sprite = Sprite ( '<STR_LIT>' , ( x // <NUM_LIT:2> , y // <NUM_LIT:2> ) ) <EOL> self . sprite2 = Sprite ( '<STR_LIT>' , ( x // <NUM_LIT:4> , y // <NUM_LIT:2> ) ) <EOL> self . add ( self . sprite ) <EOL> self . add ( self . sprite2 , name = "<STR_LIT>" ) <EOL> def rem ( ) : <EOL> self . remove ( self . sprite ) <EOL> self . remove ( "<STR_LIT>" ) <EOL> self . do ( Delay ( <NUM_LIT:5> ) + CallFunc ( rem ) ) <EOL> def main ( ) : <EOL> director . init ( ) <EOL> test_layer = TestLayer ( ) <EOL> main_scene = cocos . scene . Scene ( test_layer ) <EOL> director . run ( main_scene ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
<s> from __future__ import division , print_function , unicode_literals <EOL> import sys <EOL> import os <EOL> sys . path . insert ( <NUM_LIT:0> , os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT:..>' ) ) <EOL> testinfo = "<STR_LIT>" <EOL> tags = "<STR_LIT>" <EOL> import cocos <EOL> from cocos . director import director <EOL> from cocos . sprite import Sprite <EOL> from cocos . actions import Place , MoveBy , Hide <EOL> import pyglet <EOL> class TestLayer ( cocos . layer . Layer ) : <EOL> def __init__ ( self ) : <EOL> super ( TestLayer , self ) . __init__ ( ) <EOL> x , y = director . get_window_size ( ) <EOL> self . sprite = Sprite ( '<STR_LIT>' , ( <NUM_LIT:0> , y // <NUM_LIT:2> ) ) <EOL> self . add ( self . sprite ) <EOL> self . sprite . do ( MoveBy ( ( x // <NUM_LIT:2> , <NUM_LIT:0> ) ) + Hide ( ) ) <EOL> description = """<STR_LIT>""" <EOL> def main ( ) : <EOL> print ( description ) <EOL> director . init ( ) <EOL> test_layer = TestLayer ( ) <EOL> main_scene = cocos . scene . Scene ( test_layer ) <EOL> director . run ( main_scene ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
<s> from __future__ import division , print_function , unicode_literals <EOL> import sys <EOL> import os <EOL> sys . path . insert ( <NUM_LIT:0> , os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT:..>' ) ) <EOL> testinfo = "<STR_LIT>" <EOL> tags = "<STR_LIT>" <EOL> autotest = <NUM_LIT:0> <EOL> import cocos <EOL> from cocos . actions import Delay , CallFunc <EOL> from cocos . director import director <EOL> class HelloWorld ( cocos . layer . Layer ) : <EOL> is_event_handler = True <EOL> def __init__ ( self ) : <EOL> super ( HelloWorld , self ) . __init__ ( ) <EOL> self . label = cocos . text . Label ( '<STR_LIT>' , <EOL> font_name = '<STR_LIT>' , <EOL> font_size = <NUM_LIT:32> , <EOL> x = <NUM_LIT> , y = <NUM_LIT> , <EOL> anchor_x = '<STR_LIT>' , anchor_y = '<STR_LIT>' ) <EOL> self . add ( self . label ) <EOL> if autotest : <EOL> self . do ( Delay ( <NUM_LIT:1> ) + CallFunc ( self . move_label , <NUM_LIT:100> , <NUM_LIT:100> ) + <EOL> Delay ( <NUM_LIT:1> ) + CallFunc ( self . move_label , <NUM_LIT> , <NUM_LIT> ) ) <EOL> def on_mouse_motion ( self , x , y , dx , dy ) : <EOL> if not autotest : <EOL> vh , vy = director . get_virtual_coordinates ( x , y ) <EOL> self . move_label ( vh , vy ) <EOL> def move_label ( self , x , y ) : <EOL> self . label . element . text = '<STR_LIT>' % ( x , y ) <EOL> self . label . element . x = x <EOL> self . label . element . y = y <EOL> description = """<STR_LIT>""" <EOL> def main ( ) : <EOL> print ( description ) <EOL> director . init ( ) <EOL> hello_layer = HelloWorld ( ) <EOL> main_scene = cocos . scene . Scene ( hello_layer ) <EOL> director . run ( main_scene ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
<s> from __future__ import division , print_function , unicode_literals <EOL> import sys <EOL> import os <EOL> sys . path . insert ( <NUM_LIT:0> , os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT:..>' ) ) <EOL> testinfo = "<STR_LIT>" <EOL> tags = "<STR_LIT>" <EOL> import cocos <EOL> from cocos . director import director <EOL> from cocos . actions import * <EOL> from cocos . layer import * <EOL> from cocos . scenes import * <EOL> t0 = <NUM_LIT:0.0> <EOL> time_x = <NUM_LIT:0.0> <EOL> scene1 = None <EOL> scene2 = None <EOL> scene3 = None <EOL> stage = None <EOL> last_current_scene = <NUM_LIT> <EOL> def report ( t ) : <EOL> global stage , scene1 , scene2 <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' % t ) <EOL> print ( '<STR_LIT>' , len ( director . scene_stack ) ) <EOL> current_scene = director . scene <EOL> if current_scene is None : <EOL> s_scene = '<STR_LIT:None>' <EOL> elif current_scene is scene1 : <EOL> s_scene = '<STR_LIT>' <EOL> elif current_scene is scene2 : <EOL> s_scene = '<STR_LIT>' <EOL> else : <EOL> s_scene = '<STR_LIT>' <EOL> print ( '<STR_LIT>' , s_scene , current_scene ) <EOL> def sequencer ( dt ) : <EOL> global time_x , t0 , stage , last_current_scene <EOL> time_x += dt <EOL> if last_current_scene != director . scene : <EOL> last_current_scene = director . scene <EOL> report ( time_x ) <EOL> if stage == "<STR_LIT>" and time_x > <NUM_LIT> : <EOL> stage = "<STR_LIT>" <EOL> print ( "<STR_LIT>" % ( time_x , stage ) ) <EOL> director . push ( FadeTransition ( scene2 , <NUM_LIT:0.5> ) ) <EOL> elif stage == "<STR_LIT>" and time_x > <NUM_LIT> : <EOL> stage = "<STR_LIT>" <EOL> print ( "<STR_LIT>" % ( time_x , stage ) ) <EOL> director . replace ( FadeTransitionWithPop ( director . scene_stack [ <NUM_LIT:0> ] , <NUM_LIT:0.5> ) ) <EOL> class FadeTransitionWithPop ( FadeTransition ) : <EOL> def finish ( self ) : <EOL> director . pop ( ) <EOL> class ZoomTransitionWithPop ( ZoomTransition ) : <EOL> def finish ( self ) : <EOL> director . pop ( ) <EOL> class FlipX3DTransitionWithPop ( FlipX3DTransition ) : <EOL> def finish ( self ) : <EOL> director . pop ( ) <EOL> TransitionWithPop = """<STR_LIT>""" <EOL> description = """<STR_LIT>""" <EOL> class TestScene ( cocos . scene . Scene ) : <EOL> def on_enter ( self ) : <EOL> super ( TestScene , self ) . on_enter ( ) <EOL> self . schedule ( sequencer ) <EOL> def on_exit ( self ) : <EOL> self . unschedule ( sequencer ) <EOL> def main ( ) : <EOL> global t0 , scene1 , scene2 , scene3 , stage <EOL> print ( description ) <EOL> print ( "<STR_LIT>" ) <EOL> director . init ( resizable = True ) <EOL> scene1 = TestScene ( ) <EOL> scene1 . add ( ColorLayer ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:32> , <NUM_LIT:255> ) ) <EOL> scene2 = TestScene ( ) <EOL> scene2 . add ( ColorLayer ( <NUM_LIT> , <NUM_LIT:32> , <NUM_LIT> , <NUM_LIT:255> ) ) <EOL> stage = "<STR_LIT>" <EOL> print ( "<STR_LIT>" % ( <NUM_LIT:0.0> , stage ) ) <EOL> report ( <NUM_LIT:0> ) <EOL> stage = "<STR_LIT>" <EOL> print ( "<STR_LIT>" % ( <NUM_LIT:0.0> , stage ) ) <EOL> director . run ( scene1 ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
<s> from __future__ import division , print_function , unicode_literals <EOL> import sys <EOL> import os <EOL> sys . path . insert ( <NUM_LIT:0> , os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' ) ) <EOL> import math <EOL> from math import pi , atan <EOL> try : <EOL> import cPickle as pickle <EOL> except ImportError : <EOL> import pickle <EOL> import glob <EOL> from optparse import OptionParser <EOL> import pyglet <EOL> from pyglet . gl import * <EOL> from pyglet . window import key <EOL> import cocos <EOL> from cocos . director import director <EOL> from cocos . sprite import Sprite <EOL> from cocos . actions import CallFuncS , CallFunc , IntervalAction <EOL> from cocos import euclid <EOL> import ui <EOL> from cocos . skeleton import Bone , Skeleton , Skin , Animation , Animate , BitmapSkin , ColorSkin <EOL> def flatten ( l ) : <EOL> res = [ ] <EOL> for e in l : <EOL> res += list ( e ) <EOL> return res <EOL> def v2a ( x , y ) : <EOL> if x == <NUM_LIT:0> : <EOL> if y > <NUM_LIT:0> : <EOL> return pi / <NUM_LIT:2> <EOL> elif ( y < <NUM_LIT:0> ) : <EOL> return - pi / <NUM_LIT:2> <EOL> else : <EOL> return <NUM_LIT:0> <EOL> elif y == <NUM_LIT:0> : <EOL> if x > <NUM_LIT:0> : <EOL> return <NUM_LIT:0> <EOL> else : <EOL> return pi <EOL> else : <EOL> if x < <NUM_LIT:0> : <EOL> if y > <NUM_LIT:0> : <EOL> return atan ( y / x ) + pi <EOL> else : <EOL> return atan ( y / x ) - pi <EOL> else : <EOL> return atan ( y / x ) <EOL> class UpdateTimeline ( cocos . actions . IntervalAction ) : <EOL> def init ( self , duration ) : <EOL> self . duration = duration <EOL> def update ( self , t ) : <EOL> self . target . position = self . duration * t <EOL> def __reversed__ ( self ) : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> class BoneControl ( ui . BallWidget ) : <EOL> def __init__ ( self , bone , delta ) : <EOL> super ( BoneControl , self ) . __init__ ( <NUM_LIT:7> , ( <NUM_LIT:255> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:255> ) ) <EOL> self . bone = bone <EOL> self . position = bone . get_end ( ) + euclid . Point2 ( * delta ) <EOL> self . delta = delta <EOL> def on_dragged ( self , dx , dy ) : <EOL> super ( BoneControl , self ) . on_dragged ( dx , dy ) <EOL> def angle_between ( v1 , v2 ) : <EOL> v1 = v1 . normalized ( ) <EOL> v2 = v2 . normalized ( ) <EOL> a1 = v2a ( * v1 ) <EOL> a2 = v2a ( * v2 ) <EOL> return a2 - a1 <EOL> o = self . bone . get_start ( ) <EOL> e = self . bone . get_end ( ) <EOL> ne = euclid . Point2 ( <EOL> self . x + dx - self . delta [ <NUM_LIT:0> ] , <EOL> self . y + dy - self . delta [ <NUM_LIT:1> ] ) <EOL> v1 = euclid . Point2 ( * ( e - o ) ) <EOL> v2 = euclid . Point2 ( * ( ne - o ) ) <EOL> alpha = angle_between ( v1 , v2 ) <EOL> self . bone . rotate ( alpha ) <EOL> class SkeletonControl ( ui . BallWidget ) : <EOL> def __init__ ( self , skeleton , position ) : <EOL> super ( SkeletonControl , self ) . __init__ ( <NUM_LIT:7> , ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:255> , <NUM_LIT:255> ) ) <EOL> self . skeleton = skeleton <EOL> self . position = position + skeleton . translation <EOL> def on_dragged ( self , dx , dy ) : <EOL> super ( SkeletonControl , self ) . on_dragged ( dx , dy ) <EOL> self . skeleton . move ( dx , dy ) <EOL> class BoneUILayer ( ui . UILayer ) : <EOL> def __init__ ( self , skeleton , savefile_name , skin = False ) : <EOL> super ( BoneUILayer , self ) . __init__ ( ) <EOL> self . user_skin = skin <EOL> self . count = <NUM_LIT:0> <EOL> self . savefile_name = savefile_name <EOL> try : <EOL> self . animation = pickle . load ( open ( savefile_name , "<STR_LIT:rb>" ) ) <EOL> except IOError : <EOL> self . animation = Animation ( skeleton ) <EOL> self . timeline = ui . TimeLine ( self . animation ) <EOL> self . add ( self . timeline ) <EOL> self . tick_delta = <NUM_LIT:1.0> / <NUM_LIT:16> <EOL> self . skeleton = skeleton <EOL> self . editable_skeleton = None <EOL> self . animating = False <EOL> self . animation . move_start ( ) <EOL> self . update_visual ( ) <EOL> def save ( self ) : <EOL> pickle . dump ( self . animation , open ( self . savefile_name , "<STR_LIT:wb>" ) ) <EOL> def start_animation ( self ) : <EOL> self . clean_skins ( ) <EOL> self . animating = True <EOL> self . clean_control_points ( ) <EOL> if self . user_skin : <EOL> skin = BitmapSkin ( self . skeleton , self . user_skin ) <EOL> else : <EOL> skin = ColorSkin ( self . skeleton , ( <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> ) ) <EOL> self . add ( skin ) <EOL> xs , ys = director . get_window_size ( ) <EOL> skin . position = xs / <NUM_LIT:2> - <NUM_LIT:6> , ys / <NUM_LIT:2> - <NUM_LIT:11> <EOL> self . animation . move_start ( ) <EOL> skin . do ( Animate ( self . animation ) <EOL> + CallFunc ( lambda : self . remove ( skin ) ) <EOL> + CallFunc ( self . stop_animation ) ) <EOL> skin . do ( UpdateTimeline ( self . animation . get_duration ( ) ) , target = self . animation ) <EOL> def stop_animation ( self ) : <EOL> self . animating = False <EOL> self . update_visual ( ) <EOL> def add_skin_for_skeleton ( self , skeleton , color , z = - <NUM_LIT:1> , editable = False ) : <EOL> if self . user_skin : <EOL> skin = BitmapSkin ( skeleton , self . user_skin , alpha = color [ <NUM_LIT:3> ] ) <EOL> else : <EOL> skin = ColorSkin ( skeleton , color ) <EOL> self . skin = skin <EOL> self . add ( skin , z = z ) <EOL> xs , ys = director . get_window_size ( ) <EOL> skin . position = xs / <NUM_LIT:2> - <NUM_LIT:6> , ys / <NUM_LIT:2> - <NUM_LIT:11> <EOL> if editable : <EOL> self . editable_skeleton = skeleton <EOL> self . editable_skin = skin <EOL> self . generate_control_points ( ) <EOL> def on_key_press ( self , k , mods ) : <EOL> if not self . animating : <EOL> if k == key . S : <EOL> self . save ( ) <EOL> self . count += <NUM_LIT:1> <EOL> elif k == key . LEFT : <EOL> self . animation . move_position ( - self . tick_delta ) <EOL> elif k == key . RIGHT : <EOL> self . animation . move_position ( self . tick_delta ) <EOL> elif k in ( key . PLUS , key . NUM_ADD ) : <EOL> self . animation . insert_keyframe ( ) <EOL> elif k in ( key . MINUS , key . NUM_SUBTRACT ) : <EOL> self . animation . remove_keyframe ( ) <EOL> elif k == key . PAGEDOWN : <EOL> self . animation . next_keyframe ( ) <EOL> elif k == key . PAGEUP : <EOL> self . animation . prev_keyframe ( ) <EOL> elif k == key . INSERT : <EOL> self . animation . insert_time ( self . tick_delta ) <EOL> elif k == key . DELETE : <EOL> self . animation . delete_time ( self . tick_delta ) <EOL> elif k == key . HOME : <EOL> self . animation . move_start ( ) <EOL> elif k == key . END : <EOL> self . animation . move_end ( ) <EOL> self . update_visual ( ) <EOL> if k == key . SPACE : <EOL> self . start_animation ( ) <EOL> else : <EOL> pass <EOL> def update_visual ( self ) : <EOL> self . editable_skeleton = None <EOL> self . clean_control_points ( ) <EOL> self . clean_skins ( ) <EOL> _ , curr = self . animation . get_keyframe ( ) <EOL> _ , prev = self . animation . get_keyframe ( - <NUM_LIT:1> ) <EOL> _ , prev2 = self . animation . get_keyframe ( - <NUM_LIT:2> ) <EOL> _ , next = self . animation . get_keyframe ( <NUM_LIT:1> ) <EOL> if curr : <EOL> self . add_skin_for_skeleton ( curr , ( <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> ) , - <NUM_LIT:1> , True ) <EOL> if prev : <EOL> self . add_skin_for_skeleton ( prev , ( <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:32> ) , - <NUM_LIT:2> ) <EOL> if prev2 : <EOL> self . add_skin_for_skeleton ( prev2 , ( <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:16> ) , - <NUM_LIT:3> ) <EOL> if next : <EOL> self . add_skin_for_skeleton ( next , ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:255> , <NUM_LIT:32> ) , - <NUM_LIT:2> ) <EOL> def clean_control_points ( self ) : <EOL> cps = [ cp for cp in self . get_children ( ) if isinstance ( cp , ui . BallWidget ) ] <EOL> for cp in cps : <EOL> self . remove ( cp ) <EOL> def clean_skins ( self ) : <EOL> skins = [ cp for cp in self . get_children ( ) if isinstance ( cp , Skin ) ] <EOL> for skin in skins : <EOL> self . remove ( skin ) <EOL> def on_mouse_release ( self , * args ) : <EOL> if self . dragging : <EOL> self . clean_control_points ( ) <EOL> self . generate_control_points ( ) <EOL> super ( BoneUILayer , self ) . on_mouse_release ( * args ) <EOL> def on_mouse_drag ( self , x , y , dx , dy , button , modifiers ) : <EOL> if self . hovering : <EOL> cps = [ cp for cp in self . get_children ( ) <EOL> if isinstance ( cp , ui . BallWidget ) and cp != self . hovering ] <EOL> for cp in cps : <EOL> self . remove ( cp ) <EOL> super ( BoneUILayer , self ) . on_mouse_drag ( x , y , dx , dy , button , modifiers ) <EOL> def generate_control_points ( self ) : <EOL> if self . editable_skeleton : <EOL> skinpos = euclid . Point2 ( * self . editable_skin . position ) <EOL> for cp in self . editable_skeleton . get_control_points ( ) : <EOL> if isinstance ( cp , Skeleton ) : <EOL> self . add ( SkeletonControl ( cp , skinpos ) ) <EOL> else : <EOL> self . add ( BoneControl ( cp , skinpos ) ) <EOL> class Editor ( cocos . scene . Scene ) : <EOL> def __init__ ( self , skeleton , savefile_name , skin = False ) : <EOL> super ( Editor , self ) . __init__ ( ) <EOL> self . ui = BoneUILayer ( skeleton , savefile_name , skin ) <EOL> self . add ( self . ui ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> import sys , imp <EOL> director . init ( ) <EOL> parser = OptionParser ( ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" , default = False , metavar = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" , default = <NUM_LIT:1> , metavar = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" , default = False , metavar = "<STR_LIT>" ) <EOL> ( options , args ) = parser . parse_args ( ) <EOL> def usage ( ) : <EOL> return "<STR_LIT>" <EOL> if len ( args ) != <NUM_LIT:2> : <EOL> print ( usage ( ) ) <EOL> print ( parser . error ( "<STR_LIT>" ) ) <EOL> sys . exit ( ) <EOL> sk_file = imp . load_source ( "<STR_LIT>" , args [ <NUM_LIT:0> ] ) <EOL> if options . skin : <EOL> skin_data = imp . load_source ( "<STR_LIT>" , options . skin ) . skin <EOL> options . skin = skin_data <EOL> animator = Editor ( sk_file . skeleton , args [ <NUM_LIT:1> ] , options . skin ) <EOL> if options . background : <EOL> background = cocos . sprite . Sprite ( options . background ) <EOL> x , y = director . get_window_size ( ) <EOL> animator . add ( background , z = - <NUM_LIT:10> ) <EOL> background . position = x / <NUM_LIT:2> , y / <NUM_LIT:2> <EOL> background . scale = float ( options . scale ) <EOL> director . run ( animator ) </s>
<s> from __future__ import division , print_function , unicode_literals <EOL> import os <EOL> assert os . environ [ '<STR_LIT>' ] <EOL> import sys <EOL> sys . path . insert ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> import pyglet <EOL> assert pyglet . mock_level == <NUM_LIT:1> <EOL> import cocos <EOL> import cocos . actions . instant_actions <EOL> from cocos . cocosnode import CocosNode <EOL> from cocos . director import director <EOL> director . init ( ) <EOL> def test_Loop_InstantAction ( ) : <EOL> rec = [ ] <EOL> def f ( x ) : <EOL> rec . append ( x ) <EOL> node = CocosNode ( ) <EOL> template_action = cocos . actions . instant_actions . CallFunc ( f , <NUM_LIT:1> ) <EOL> n = <NUM_LIT:5> <EOL> action = template_action * n <EOL> node . do ( action ) <EOL> assert rec == [ <NUM_LIT:1> ] * n </s>
<s> from __future__ import unicode_literals <EOL> import logging <EOL> import random <EOL> from django . core . exceptions import SuspiciousOperation <EOL> from django . core . signing import b64_decode <EOL> from django . utils . crypto import get_random_string <EOL> from django . utils . encoding import force_bytes , force_text <EOL> from django . utils . six import binary_type , string_types <EOL> from debreach . utils import xor <EOL> log = logging . getLogger ( __name__ ) <EOL> class CSRFCryptMiddleware ( object ) : <EOL> def _decode ( self , token ) : <EOL> key , value = force_bytes ( token , encoding = '<STR_LIT>' ) . split ( b'<STR_LIT:$>' , <NUM_LIT:1> ) <EOL> return force_text ( xor ( b64_decode ( value ) , key ) , encoding = '<STR_LIT>' ) <EOL> def process_view ( self , request , view , view_args , view_kwargs ) : <EOL> if getattr ( view , '<STR_LIT>' , False ) : <EOL> return None <EOL> if request . POST . get ( '<STR_LIT>' ) and '<STR_LIT:$>' in request . POST . get ( '<STR_LIT>' ) : <EOL> try : <EOL> post_was_mutable = request . POST . _mutable <EOL> POST = request . POST . copy ( ) <EOL> token = POST . get ( '<STR_LIT>' ) <EOL> POST [ '<STR_LIT>' ] = self . _decode ( token ) <EOL> POST . _mutable = post_was_mutable <EOL> request . POST = POST <EOL> except : <EOL> log . exception ( '<STR_LIT>' ) <EOL> raise SuspiciousOperation ( <EOL> '<STR_LIT>' ) <EOL> if request . META . get ( '<STR_LIT>' ) and '<STR_LIT:$>' in request . META . get ( '<STR_LIT>' ) : <EOL> try : <EOL> META = request . META . copy ( ) <EOL> token = META . get ( '<STR_LIT>' ) <EOL> META [ '<STR_LIT>' ] = self . _decode ( token ) <EOL> request . META = META <EOL> except : <EOL> log . exception ( '<STR_LIT>' ) <EOL> raise SuspiciousOperation ( <EOL> '<STR_LIT>' ) <EOL> return None <EOL> class RandomCommentMiddleware ( object ) : <EOL> def process_response ( self , request , response ) : <EOL> str_types = string_types + ( binary_type , ) <EOL> if not getattr ( response , '<STR_LIT>' , False ) and response . get ( '<STR_LIT:Content-Type>' , '<STR_LIT>' ) . startswith ( '<STR_LIT>' ) and response . content and isinstance ( response . content , str_types ) and not getattr ( response , '<STR_LIT>' , False ) and not getattr ( response , '<STR_LIT>' , False ) : <EOL> comment = '<STR_LIT>' . format ( <EOL> get_random_string ( random . choice ( range ( <NUM_LIT:12> , <NUM_LIT> ) ) ) ) <EOL> response . content = '<STR_LIT>' . format ( <EOL> force_text ( response . content ) , comment ) <EOL> response . _random_comment_applied = True <EOL> return response </s>