<s> import datetime <EOL> import io , csv , json <EOL> from sqlalchemy import Column , Integer , String , DateTime , Float , Text <EOL> from db import Base <EOL> from psiturk_config import PsiturkConfig <EOL> config = PsiturkConfig ( ) <EOL> config . load_config ( ) <EOL> TABLENAME = config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> CODE_VERSION = config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class Participant ( Base ) : <EOL> """<STR_LIT>""" <EOL> __tablename__ = TABLENAME <EOL> uniqueid = Column ( String ( <NUM_LIT> ) , primary_key = True ) <EOL> assignmentid = Column ( String ( <NUM_LIT> ) , nullable = False ) <EOL> workerid = Column ( String ( <NUM_LIT> ) , nullable = False ) <EOL> hitid = Column ( String ( <NUM_LIT> ) , nullable = False ) <EOL> ipaddress = Column ( String ( <NUM_LIT> ) ) <EOL> browser = Column ( String ( <NUM_LIT> ) ) <EOL> platform = Column ( String ( <NUM_LIT> ) ) <EOL> language = Column ( String ( <NUM_LIT> ) ) <EOL> cond = Column ( Integer ) <EOL> counterbalance = Column ( Integer ) <EOL> codeversion = Column ( String ( <NUM_LIT> ) ) <EOL> beginhit = Column ( DateTime ) <EOL> beginexp = Column ( DateTime ) <EOL> endhit = Column ( DateTime ) <EOL> bonus = Column ( Float , default = <NUM_LIT:0> ) <EOL> status = Column ( Integer , default = <NUM_LIT:1> ) <EOL> if '<STR_LIT>' in config . get ( '<STR_LIT>' , '<STR_LIT>' ) . lower ( ) : <EOL> datastring = Column ( Text ) <EOL> else : <EOL> datastring = Column ( Text ( <NUM_LIT> ) ) <EOL> def __init__ ( self , ** kwargs ) : <EOL> self . uniqueid = "<STR_LIT>" . format ( ** kwargs ) <EOL> for key in kwargs : <EOL> setattr ( self , key , kwargs [ key ] ) <EOL> self . status = <NUM_LIT:1> <EOL> self . codeversion = CODE_VERSION <EOL> self . beginhit = datetime . datetime . now ( ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( <EOL> self . uniqueid , <EOL> self . cond , <EOL> self . status , <EOL> self . codeversion ) <EOL> def get_trial_data ( self ) : <EOL> try : <EOL> trialdata = json . loads ( self . datastring ) [ "<STR_LIT:data>" ] <EOL> except TypeError , ValueError : <EOL> print ( "<STR_LIT>" , self ) <EOL> return ( "<STR_LIT>" ) <EOL> try : <EOL> ret = [ ] <EOL> with io . BytesIO ( ) as outstring : <EOL> csvwriter = csv . writer ( outstring ) <EOL> for trial in trialdata : <EOL> csvwriter . writerow ( ( <EOL> self . uniqueid , <EOL> trial [ "<STR_LIT>" ] , <EOL> trial [ "<STR_LIT>" ] , <EOL> json . dumps ( trial [ "<STR_LIT>" ] ) ) ) <EOL> ret = outstring . getvalue ( ) <EOL> return ret <EOL> except : <EOL> print ( "<STR_LIT>" , self ) <EOL> return ( "<STR_LIT>" ) <EOL> def get_event_data ( self ) : <EOL> try : <EOL> eventdata = json . loads ( self . datastring ) [ "<STR_LIT>" ] <EOL> except ValueError , TypeError : <EOL> print ( "<STR_LIT>" , self ) <EOL> return ( "<STR_LIT>" ) <EOL> try : <EOL> ret = [ ] <EOL> with io . BytesIO ( ) as outstring : <EOL> csvwriter = csv . writer ( outstring ) <EOL> for event in eventdata : <EOL> csvwriter . writerow ( ( self . uniqueid , event [ "<STR_LIT>" ] , event [ "<STR_LIT>" ] , event [ "<STR_LIT:value>" ] , event [ "<STR_LIT>" ] ) ) <EOL> ret = outstring . getvalue ( ) <EOL> return ret <EOL> except : <EOL> print ( "<STR_LIT>" , self ) <EOL> return ( "<STR_LIT>" ) <EOL> def get_question_data ( self ) : <EOL> try : <EOL> questiondata = json . loads ( self . datastring ) [ "<STR_LIT>" ] <EOL> except TypeError , ValueError : <EOL> print ( "<STR_LIT>" , self ) <EOL> return ( "<STR_LIT>" ) <EOL> try : <EOL> ret = [ ] <EOL> with io . BytesIO ( ) as outstring : <EOL> csvwriter = csv . writer ( outstring ) <EOL> for question in questiondata : <EOL> csvwriter . writerow ( ( self . uniqueid , question , questiondata [ question ] ) ) <EOL> ret = outstring . getvalue ( ) <EOL> return ret <EOL> except : <EOL> print ( "<STR_LIT>" , self ) <EOL> return ( "<STR_LIT>" ) </s>
