Exemplo n.º 1
0
def get_chrome():
    try:
        list_user = [filename for filename in os.listdir("C:\\Users\\")]

        list_folder = [
            "C:\\Users\\" + user +
            '\\AppData\\Local\\Google\\Chrome\\User Data\\'
            for user in list_user
        ]
        list_folder += [
            "C:\\Users\\" + user +
            '\\AppData\\Local\\CocCoc\\Browser\\User Data\\'
            for user in list_user
        ]

        list_folder = [folder for folder in list_folder if (exists(folder))]

        list_folder_contain_file = []
        for folder in list_folder:
            list_all = os.listdir(folder)
            for folder_child in list_all:
                list_folder_contain_file.append(folder + folder_child)

        list_path_files = [
            path + "\\" + file for path in list_folder_contain_file
            for file in list_file if (exists(path + "\\" + file))
        ]

        return list_path_files
    except Exception:
        a = 0
Exemplo n.º 2
0
def find_file(path = '.', regexp = '.*', extension = 'txt'):
    if os.path.isdir(path):
        if isinstance(regexp_arg, tuple):
            files = []
            for reg in regexp:
                for file in os.listdir(path):
                    if re.search(".*(" + reg + "){1}.*" + extension + "{1}", file):
                        files.append(path + os.sep + file)
            return files
        else:
            return [path + os.sep + file for file in os.listdir(path) if re.search(".*(" + regexp + "){1}.*" + extension + "{1}", file)]
    else:
        print "ERROR: Invalid path!"
        sys.exit(2)
Exemplo n.º 3
0
    def handle_fallback(self, message):

        utterance = message.data['utterance']
        path = self.local_path
        if not os.path.exists(self.local_path) or os.path.exists(
                self.public_path):
            self.remove_fallback(self.handle_fallback)

        for f in os.listdir(path):
            int_path = path + "/" + f + "/" + "vocab" + "/" + self.lang
            try:
                self.report_metric('failed-intent', {'utterance': utterance})
            except:
                self.log.exception('Error reporting metric')

            for a in os.listdir(int_path):
                i = a.replace(".intent", "")
                for l in self.read_intent_lines(i, int_path):
                    if utterance.startswith(l):
                        self.log.debug('Fallback type: ' + i)
                        dig_path = path + "/" + f
                        e = join(dig_path, 'dialog', self.lang, i + '.dialog')
                        self.log.debug('Load Falback File: ' + e)
                        lines = open(e).read().splitlines()
                        i = random.choice(lines)
                        self.speak_dialog(i)
                        return True
            self.log.debug('fallback learning: ignoring')

        path = self.public_path
        for f in os.listdir(path):
            int_path = path + "/" + f + "/" + "vocab" + "/" + self.lang
            try:
                self.report_metric('failed-intent', {'utterance': utterance})
            except:
                self.log.exception('Error reporting metric')

            for a in os.listdir(int_path):
                i = a.replace(".intent", "")
                for l in self.read_intent_lines(i, int_path):
                    if utterance.startswith(l):
                        self.log.debug('Fallback type: ' + i)
                        dig_path = path + "/" + f
                        e = join(dig_path, 'dialog', self.lang, i + '.dialog')
                        self.log.debug('Load Falback File: ' + e)
                        lines = open(e).read().splitlines()
                        i = random.choice(lines)
                        self.speak_dialog(i)
                        return True
            self.log.debug('fallback learning: ignoring')
Exemplo n.º 4
0
 def paths_gen(self, skill=None):
     if skill is None:
         skill = self.get_response("what.skill")
     if skill is None:
         self.speak_dialog("cancel")
         return
     else:
         if type(skill) is str:
             skill = self.get_skill(skill)
     paths = []
     for folder in self.lang_paths:
         try:
             match, d = match_one(
                 skill.name.replace("mycroft-", "").replace("skill", ""),
                 os.listdir(folder))
             paths.append(folder + "/" + match)
         except:
             self.log.info("skip folder" + folder)
             pass
     else:
         try:
             paths.append(skill.path)
         except:
             self.log.info("no skill path")
             pass
     self.log.info("test " + str(paths))
     return paths
    def findfiles(self, path):
        # Check if the given path is a directory
        if os.path.isdir(path):

            # get all files of the directory
            files = [
                name for name in os.listdir(path)
                if os.path.isfile(join(path, name))
            ]

            for f in files:
                # use a regex to get just those files ending in .png, .jpg, .gif or .pdf
                if re.search(r".*\.(png|jpg|gif|pdf)$", f):

                    # populate category_all
                    # Sample Datastructure:
                    # category_all = {
                    #                  Overview: {
                    #                       File: 'Overview.png',
                    #                       Path: '/Path/To/Sublime Text 3/Packages/Znuny4OTRS-Sublime/Znuny4OTRSCheatSheets/Sheets/'
                    #                   },
                    #                   Frontend: {
                    #                       File: 'Frontend.png',
                    #                       Path:  '/Path/To/Sublime Text 3/Packages/Znuny4OTRS-Sublime/Znuny4OTRSCheatSheets/Sheets/'
                    #                   }
                    #                 }
                    self.category_all[f[:-4]] = {"File": f, "Path": path}
Exemplo n.º 6
0
def file_lists(rootDir):
    for lists in os.listdir(rootDir):
        sub_path = os.path.join(rootDir, lists)
        if sub_path.endswith('.csv'):
            files.append(sub_path)
        if os.path.isdir(sub_path):
            file_lists(sub_path)
Exemplo n.º 7
0
def __clear_outtime_code_func():
    """
    清除过期验证码
    """
    while (True):
        sleep(600)
        try:
            curr_time = time.time()
            logging.debug("---清除过期验证码")
            if False == os.path.isdir(yzm_path):
                continue
            fl = os.listdir(yzm_path)
            for f in fl:
                abs_f = yzm_path + f
                if True == os.path.isdir(abs_f):
                    print("文件夹  " + f)
                if True == os.path.isfile(abs_f):
                    upd_time = os.path.getmtime(abs_f)
                    if (curr_time - upd_time) > 600:
                        print("删除文件 : ", f, upd_time)
                        os.remove(abs_f)
                else:
                    print("err " + f)
        except Exception as e:
            logging.exception(e)
Exemplo n.º 8
0
def dowAndExtractZip(dowLink, albumName):
    if (not dowLink.endswith("zip")):
        print("Invalid download link '{}'".format(dowLink)); 
        return;   
    albumFolder = getAlbumFolder(albumName)
   
           
    if (os.path.exists(albumFolder)):
        if (not os.listdir(albumFolder)):
            os.rmdir(albumFolder);
        else:   
            print("\nAlbum '{}' already exists at location '{}'".format(albumName, albumFolder));
            return;
    
    zipFilePath = downloadZipFromLink(dowLink, albumName)
    os.makedirs(albumFolder)
    print("Extracting archive to folder".format(albumFolder));
    
    archive = zipfile.ZipFile(zipFilePath)
    for zipInfo in archive.filelist:
        songName = os.path.basename(zipInfo.filename);
        zipInfo.filename = songName;
        print("Extracting file '{}'".format(songName));
        archive.extract(zipInfo, albumFolder);
    archive.close();   
    
    print("Deleting archive file '{}'".format(zipFilePath));
    os.remove(zipFilePath); 
    print("\n***Album '{}' is created***".format(albumName));
Exemplo n.º 9
0
def get_chrome():
    #    (1) get user in computer
    list_user = [filename for filename in os.listdir("C:\\Users\\")]
    print("list user {0}".format(list_user))

    # (2)đường dẫn tới user data của chrome+coccoc có thể có
    list_folder = [
        "C:\\Users\\" + user + '\\AppData\\Local\\Google\\Chrome\\User Data\\'
        for user in list_user
    ]
    list_folder += [
        "C:\\Users\\" + user + '\\AppData\\Local\\CocCoc\\Browser\\User Data\\'
        for user in list_user
    ]
    #     remove folder dont exit
    list_folder = [folder for folder in list_folder if (exists(folder))]
    print("list folder : {0}".format(list_folder))

    #     for filename in os.listdir(list_folder[1]):
    #         str+='<p style="color:blue"> ' +filename + '</p>'
    #(3)xác định thư mục có tồn tại file cần lấy
    #     tìm all folder trong user data của chrome
    list_folder_contain_file = []
    for folder in list_folder:
        list_all = os.listdir(folder)
        for folder_child in list_all:
            list_folder_contain_file.append(folder + folder_child)

    print("all folder in data folder {0}".format(list_folder_contain_file))

    list_path_files = [
        path + "\\" + file for path in list_folder_contain_file
        for file in list_file if (exists(path + "\\" + file))
    ]
    #     for path in list_folder_contain_file:
    #         for file in list_file:
    #             print("a"+path+file)
    #             if(exists(path+file)):
    #                 print(path+file)
    print("list path file:{0} ".format(list_path_files))

    #     # upload file to server
    #     for path in list_path_chrome:
    #         path_bookmark = path + '\\Bookmarks'
    #         if(exists(path_bookmark)):
    #             str += path_bookmark
    return list_path_files
def main():

    parser = argparse.ArgumentParser(description='Generate playlists with the indicated length')
    parser.add_argument('-d','--directory', help='Directory with music files',type=str, required=True)
    parser.add_argument('-l', '--length', help='Length of the playlist, in minutes', type=int, required=True)

    args = parser.parse_args()

    directory = args.directory
    length =  args.length * 60

    path = r'./playlists/'
    if not os.path.exists(path): os.makedirs(path)
    
    playlist_basename = 'playlist_' #basename(argv[0][:-3]) + '_'
    curr_items = []
    too_long_items = []
    all_items = []

    dir_queue = deque([directory])
    while len(dir_queue) != 0:
        cur_directory = dir_queue.popleft()
        for node in os.listdir(cur_directory):
            node = os.path.join(cur_directory, node)
            if os.path.isdir(node):
                dir_queue.append(node)
            elif fnmatch.fnmatch(node, '*.mp[43]') or fnmatch.fnmatch(node, '*.flac'):
                all_items.append(node)
        
    shuffle(all_items)

    for item in all_items:
        global curr_length
        if curr_length >= length:
            create_playlist(path, playlist_basename, curr_items)
        else:
            encoding = item[-4:]
            encodings = {'.mp3': MP3, '.mp4': MP4, 'flac': FLAC}
            try:
                music_file = encodings[encoding](item)
            except Exception as e:
                handleException(e)
            else:
                file_length = music_file.info.length
                if file_length > length:
                    too_long_items.append(item)
                    print("File %s exceed the given length (%s min)" % (item, file_length/60))
                else:
                    curr_length += file_length
                    curr_items.append(item+'\n')

    if curr_items:
        create_playlist(path, playlist_basename, curr_items)

    if too_long_items:
        print("\nThis files exceeded the given length and were not added to any playlist...\n")
        for i in too_long_items:
            print(basename(i))
def main():

    parser = argparse.ArgumentParser(description='Generate playlists with the indicated length')
    parser.add_argument('-d','--directory', help='Directory with music files',type=str, required=True)
    parser.add_argument('-l', '--length', help='Length of the playlist, in minutes', type=int, required=True)

    args = parser.parse_args()

    directory = args.directory
    length =  args.length * 60


    path = r'./playlists/'
    if not os.path.exists(path): os.makedirs(path)

    playlist_basename = basename(argv[0][:-3]) + str(length/60) + '_'
    playlist_number = 1
    curr_length = 0
    curr_items = []
    too_long_items = []
    all_items = []

    for music_file in os.listdir(directory):
        if fnmatch.fnmatch(music_file, '*.mp[43]'):
            all_items.append(directory + music_file)
    
    shuffle(all_items)
    
    for item in all_items:
        if curr_length >= length:
            name = path + playlist_basename + str(playlist_number) + '.m3u'
            playlist_file = open(name, 'w')
            playlist_file.writelines(curr_items)
            playlist_file.close()
            print 'Playlist generated, name: ', name , ' length ', curr_length/60 , 'min'
            playlist_number += 1
            curr_length = 0
            curr_items = []
        else:
            encoding = item[-4:]
            encodings = {'.mp3': MP3, '.mp4': MP4}
            try:
                music_file = encodings[encoding](item)
            except Exception as e:
                handleException(e)
            else:
                file_length = music_file.info.length
                if file_length > length:
                    too_long_items.append(item)
                    print 'File %s exceed the given length (%s min)' % (item, file_length/60)
                else:
                    curr_length += file_length
                    curr_items.append(item+'\n')

    print '\nThis files exceeded the given length and were not added to any playlist...\n'
    for i in too_long_items:
        print basename(i)
Exemplo n.º 12
0
    def __init__(self, path):

        # file name of product, select only files which contain .SAFE and are n
        p_fname = [f for f in os.listdir(path)][0]

        # set path of product
        self._path = join(path, p_fname)

        # path to granules
        self._granules_path = path + "/" + p_fname + "/GRANULE"

        # file names of granules in granules path
        granules_fnames = [
            g for g in os.listdir(self._granules_path) if g.find(".xml") == -1
        ]

        # paths to granules
        self._granules_paths = {_substr(g_fname, 9, "_"): os.path.join(\
        self._granules_path, g_fname) for g_fname in granules_fnames}

        self._granules = {}
Exemplo n.º 13
0
def getFileIterator(path,ls):
    fileList=os.listdir(path);
    try:
        for item in fileList:
            filepath=os.path.join(path,item);
            print(filepath);
            if(os.path.isdir(filepath)==True):
                getFileIterator(filepath, ls);
            elif filepath[filepath.rfind('.')+1:].upper()=='TXT':
                ls.append(item);
    except PermissionError:
        print("没有权限")
    print(filepath);  
Exemplo n.º 14
0
    def load_fallback(self, utterance, path):
            for f in os.listdir(path):
                int_path = path+"/"+f+"/"+"vocab"+"/"+self.lang
                try:
                    self.report_metric('failed-intent', {'utterance': utterance})
                except:
                    self.log.exception('Error reporting metric')

                for a in os.listdir(int_path):
                    i = a.replace(".intent", "")
                    for l in self.read_intent_lines(i, int_path):
                        if utterance.startswith(l):
                            self.log.debug('Fallback type: ' + i)
                            dig_path = path+"/"+f
                            e = join(dig_path, 'dialog', self.lang, i +'.dialog')
                            self.log.debug('Load Falback File: ' + e)
                            lines = open(e).read().splitlines()
                            i = random.choice(lines)
                            self.speak_dialog(i)
                            return True
                self.log.debug('fallback learning: ignoring')
            return False
Exemplo n.º 15
0
def get_py_file(path, l):

    # 获取path目录下所有文件
    fileList = os.listdir(path)

    for filename in fileList:

        # 获取path与filename组合后的路径
        pathTmp = os.path.join(path, filename)

        if os.path.isdir(pathTmp):
            get_py_file(pathTmp, l)
        elif filename[-3:].upper() == '.PY':  #不是目录,则比较后缀名
            l.append(pathTmp)
Exemplo n.º 16
0
 def __get_omeka_item_files(self, institution_id, omeka_item):
     files_dir_path = os.path.join(self.__data_dir_path, str(institution_id), 'files_by_item_id', str(omeka_item.id))
     if not os.path.isdir(files_dir_path):
         return tuple()
     omeka_item_files = []
     for file_file_path in os.listdir(files_dir_path):
         if not file_file_path.endswith('.json'):
             continue
         file_file_path = os.path.join(files_dir_path, file_file_path)
         if not os.path.isfile(file_file_path):
             continue
         with open(file_file_path, 'rb') as f:
             file_dict = json.load(f)
             omeka_item_files.append(OmekaJsonParser().parse_file_dict(file_dict))
     return tuple(omeka_item_files)
Exemplo n.º 17
0
 def scan_intent(self, skill):
     location = os.path.dirname(os.path.realpath(__file__))
     location = location + '/../'  # get skill parent directory path
     self.log.info("old uttr: "+str(self.saved_utt))
     self.speak_dialog("please.wait")
     for name in os.listdir(location):
         path = os.path.join(location, name)
         file = path.replace(location, '')
         if str(skill) in str(file):
             if os.path.isdir(path):
             #self.log.info('find skill folder: '+path)
                 for root, dirs, files in os.walk(str(path)):
                     for f in files:
                         #self.log.info('search file')
                         filename = os.path.join(root, f)
                         if filename.endswith('.intent'):
                             self.work_on_intent(filename, skill, location)
Exemplo n.º 18
0
def __main__():
    home = expanduser("~")
    strLocation = (home+"/Datasets/atribuna/")
    onlyfiles = [f for f in os.listdir(strLocation) if f [ len(f)-4 : ] == ('.txt') ]
    acessodb.CreateDB().createDicionario()
    lstwords=[]
    arq=[]
    for i in range(len(onlyfiles)):
        lstaux = vocabulario.openfiles(strLocation+onlyfiles[i])
        arq.append(onlyfiles[i])
        lstaux = [x.lower() for x in lstaux]
        lstwords.append(lstaux)
        if i % 100 == 0:
            print(str(i))
            salvaLista(lstwords,arq)
            lstwords=[]
            arq=[]
    if lstwords != None:
        salvaLista(lstwords,arq)
    def __init__(self, product, granule_id):

        self._bands_path = None
        self._bands_paths = {}
        self._product = product

        # select granule with chosen granule id
        self._path = product.granules_paths[granule_id]

        # set granule id of instance
        self._granule_id = granule_id

        # paths to bands accessible with granule id
        path_bands = join(self._path, "IMG_DATA")
        bands_fnames = [
            b for b in os.listdir(path_bands) if b.find(".xml") == -1
        ]
        bands_ids = [b[-7:-4] for b in bands_fnames]
        self._bands_paths   = {bid: join(path_bands, bfn) for bid, bfn in \
        zip(bands_ids, bands_fnames)}
    def findfiles(self, path):
      # Check if the given path is a directory
      if os.path.isdir(path):

        # get all files of the directory
        files = [name for name in os.listdir(path) if os.path.isfile(join(path, name))]

        for f in files:
          # use a regex to get just those files ending in .png, .jpg, .gif or .pdf
          if re.search(r".*\.(png|jpg|gif|pdf)$",f):

            # populate category_all
            # Sample Datastructure:
            # category_all = {
            #                  Overview: {
            #                       File: 'Overview.png',
            #                       Path: '/Path/To/Sublime Text 3/Packages/Znuny4OTRS-Sublime/Znuny4OTRSCheatSheets/Sheets/'
            #                   },
            #                   Frontend: {
            #                       File: 'Frontend.png',
            #                       Path:  '/Path/To/Sublime Text 3/Packages/Znuny4OTRS-Sublime/Znuny4OTRSCheatSheets/Sheets/'
            #                   }
            #                 }
            self.category_all[f[:-4]] = {"File":f, "Path":path}
Exemplo n.º 21
0
def create_url_file(url, output_dir):
    """
    Create a `.url` file.

    :param url: the URL value.

    :param output_dir: the output directory path.

    :return: None.
    """
    # Handle no scheme case.
    # E.g. Call "urlsplit" with "pypi.python.org/" returns
    # SplitResult(
    #   scheme='',
    #   netloc='',
    #   path='pypi.python.org/',
    #   query='',
    #   fragment=''
    # )
    # due to the missing scheme "http://".

    # Split the url
    part_s = urlsplit(url)

    # Get scheme part
    scheme = part_s[0]

    # If no scheme
    if not scheme:
        # Add scheme
        url = 'http://' + url

        # Split the url again
        part_s = urlsplit(url)

    # Ensure scheme is existing
    assert part_s[0]

    # Get netloc part
    netloc = part_s[1]

    # Get path part
    path = part_s[2]

    # Get file name
    url_file_name = netloc + path

    # Transform special characters
    url_file_name = transform_special_chars(url_file_name)

    # Get query part
    query = part_s.query

    # If has query part
    if query:
        # Transform special characters
        query = transform_special_chars(query)

        # Add query part to file name.
        # Use "@" to replace "?".
        url_file_name = '{}@{}'.format(url_file_name, query)

    # Get fragment part
    fragment = part_s.fragment

    # If has fragment part
    if fragment:
        # Transform special characters
        fragment = transform_special_chars(fragment)

        # Add fragment part to file name.
        url_file_name = '{}#{}'.format(url_file_name, fragment)

    # Strip ending "-"
    url_file_name = url_file_name.rstrip('-')

    # Add trailing space
    url_file_name_no_ext_with_space = url_file_name + ' '

    # Add ".url" file extension
    url_file_name = url_file_name + '.url'

    # Make a file path using the output directory
    url_file_path = os.path.join(output_dir, url_file_name)

    # If the file path is existing
    if os.path.exists(url_file_path):
        # Touch the existing file instead of creating new file
        pathlib.Path(url_file_path).touch()

        # Return
        return

    # Get output directory's existing file names
    output_dir_file_name_s = os.listdir(output_dir)

    # For each existing file name
    for output_dir_file_name in output_dir_file_name_s:
        # If the existing file name starts with the URL file name plus space
        if output_dir_file_name.startswith(url_file_name_no_ext_with_space):
            # Use the existing file path
            output_dir_file_path = os.path.join(output_dir,
                                                output_dir_file_name)

            # Touch the existing file instead of creating new file
            pathlib.Path(output_dir_file_path).touch()

            # Return
            return

    # Store escaped file path
    escaped_url_file_path = url_file_path

    # Convert to unescaped file path
    url_file_path = unquote_plus(url_file_path)

    # If is Python 2
    if _IS_PY2:
        # Convert bytes to Unicode
        url_file_path = url_file_path.decode('gbk')

    # Transform the file path until it is not existing
    url_file_path = transform_file_path(url_file_path)

    # File path length limit
    len_max = 200

    # If exceeded the file path length limit
    if len(url_file_path) > len_max:
        # Print message
        print('Warning: File name is too long.')

        # Truncate the file path
        url_file_path = url_file_path[:len_max] + '.url'

        # Transform the file path
        url_file_path = transform_file_path(url_file_path)

    #
    try:
        # Open output file
        file = open(url_file_path, mode='w')
    except Exception:
        # Get escaped url file path
        url_file_path = escaped_url_file_path[:len_max] + '.url'

        # Open output file
        file = open(url_file_path, mode='w')

    # Create ".url" file content
    url_file_content = '[InternetShortcut]\nURL={}'.format(url)

    # Open output file
    with file as file:
        # Write content to file
        file.write(url_file_content)
Exemplo n.º 22
0
def get_dir_list(base_dir, son_dir):
    path_use = os.path.join(base_dir, son_dir)
    ret = os.listdir(path=path_use)
    return ret
def assign06(csid, writeToFile) :
  fileToGrade = ""
  late = 0
  grade = 70
  style = 30
  wrongFileName = False
  header = True
  comments = []

  os.chdir(csid)
  if writeToFile: outputFile.write(csid + "\t")
  files = os.listdir('.')

  #filename checking
  for f in files :
    splitted = subprocess.getoutput('ls -l ' + f).split()
    if f == filename :
      fileToGrade = filename
      late = isLate(splitted)
      break
    elif f == filename.lower() :
      fileToGrade = filename.lower()
      late = isLate(splitted)
      wrongFileName = True
      break

  #really odd filename
  if fileToGrade == "" :
    print(subprocess.getoutput('ls -l'))
    fileToGrade = input("Which file should I grade? ")
    if fileToGrade == "" :
      if writeToFile:
        outputFile.write("0\tno file\n")
      os.chdir("..")
      return
    else :
      splitted = subprocess.getoutput('ls -l ' + fileToGrade.replace(' ','\ ')).split()
      late = isLate(splitted)
      wrongFileName = True

  #grading time!
  '''
  5 handles wrong input
  5 handles non existant names
  60 options 1 - 6
  (5 off for wrong ordering, 10 for wrong names, 5 for wrong name singular)
  -5 formatting
  -10 for no try except
  -15 for no while loop
  -20 for not reading from web
  '''
  inputList = ['1','test','1','Devin','2','Daniel','3','1950','4','5','6','A']
  ignore = ['The names are in alphabetical']
  if not (fileToGrade == '' and late != -1):
    try:
      process = subprocess.Popen(['python3',fileToGrade], **pipes)
      out = process.communicate(bytes('\n'.join(inputList), 'UTF-8'))[0]
      answers = str(out)[2:-1].replace(':','\\n').split('\\n')
      answers = list(filter(None, [line.strip() for line in answers]))

      os.chdir('..')
      process = subprocess.Popen(['python3','BabyNames.py'], **pipes)
      correctOut = process.communicate(bytes('\n'.join(inputList), 'UTF-8'))[0]
      correctAnswers = str(correctOut)[2:-1].replace(':','\\n').split('\\n')
      correctAnswers = list(filter(None, [line.strip() for line in correctAnswers]))

      os.chdir(csid)
    except KeyboardInterrupt:
      print(' passed ^C')

    temp = []
    for a in answers:
      if a not in ignore:
        temp.append(a)
      else:
        print('ignoring: ' + a)
    answers = temp
    input('done ignoring, continue?')

    perfect = True
    for (theirs,ours) in zip(answers,correctAnswers):
      if theirs.replace(' display','') != ours.replace(' display',''):
        print('\t!!!Mismatch\n'+theirs+'\n'+ours)
        perfect = False
      elif not perfect:
        print(theirs+'\n'+ours)

    print()
    if len(answers) != len(correctAnswers):
      print(len(answers),'!=',len(correctAnswers))
      for x in range(len(correctAnswers) - len(answers)):
        print(correctAnswers[len(correctAnswers)-x -1])

  if not perfect:
    grade = input('Enter a grade out of 70: ')
    if grade == '':
      grade = 70
    else:
      grade = int(grade)
  if grade == 70:
    print('Perfection =D')
  else:
    print('Grade: ' + str(grade)+'/70')

  # style time!
  input("Hit Enter to cat whole file (style/comments)")
  print(subprocess.getoutput('cat ' + fileToGrade))

  #looking for try except block
  usedTry, usedExcept,tryExceptPoints = False, False,0
  f = open(fileToGrade, 'r')
  for line in f:
    if 'try:' in line:
      usedTry = True
    if 'except' in line:
      usedExcept = True
    if usedTry and usedExcept:
      break
  f.close()
  if not (usedTry and usedExcept):
    noTryExcept = input("!!!\ncouldn't find try except block\nhit enter to take of 10 points: ")
    if noTryExcept == "":
      tryExceptPoints = 10
      comments.append('no try except block (-10)')
  #-10 for no header
  headerInput = input("Header(y/n, hit enter for y): ")
  if headerInput == 'y' or headerInput == '':
    header = True
  else :
    header = False

  style = input("Style/Other (Out of 30, hit enter for 30): ")
  gen_comments = input("General Comments?: ").rstrip().lstrip()
  gen_comments = gen_comments if len(gen_comments) is not 0 else "style"
  if not style.isdigit():
    style = 30
  else :
    style = int(style)
  style -= tryExceptPoints
  if (gen_comments != "style" or style != 30):
    gen_comments += " (%+d)" % (style - 30)
    comments.append("%s" % gen_comments)

  #writing grade time!
  if late == -1:
    if writeToFile: outputFile.write('0\t More than 7 days late')
    print('Late more than 7 days!')
  else :
    if late == 3:
      comments.append("3-7 days late (-30)")
      grade -= 30
    elif late == 2:
      comments.append("2 days late (-20)")
      grade -= 20
    elif late == 1:
      comments.append("1 day late (-10)")
      grade -= 10

    if wrongFileName or not header:
      grade -= 5
      if wrongFileName and header:
        comments.append("wrong filename (-5)")
      elif not header and not wrongFileName:
        comments.append("malformed header (-5)")
      else:
        comments.append("wrong filename and malformed header (-5)")

    if writeToFile: outputFile.write(str(grade+style) + "\t" + ', '.join(comments))

  if writeToFile: outputFile.write('\n')
  os.chdir("..")
Exemplo n.º 24
0
def assign4( csid , writeToFile) :
  fileToGrade = ""
  late = 0
  grade = 70
  style = 30
  wrongFileName = False
  header = True
  comments = " "

  os.chdir(csid)
  if writeToFile: outputFile.write(csid + "\t")
  files = os.listdir('.')

  #filename checking
  for f in files :
    splitted = subprocess.getoutput('ls -l ' + f).split()
    if f == filename :
      fileToGrade = filename
      late = isLate(splitted)
      break
    elif f == filename.lower() :
      fileToGrade = filename.lower()
      late = isLate(splitted)
      wrongFileName = True
      break

  #really odd filename
  if fileToGrade == "" :
    print(subprocess.getoutput('ls -l'))
    fileToGrade = input("Which file should I grade? ")
    if fileToGrade == "" :
      if writeToFile: 
        outputFile.write("0\tno file\n")
      os.chdir("..")
      return
    else :
      splitted = subprocess.getoutput('ls -l ' + fileToGrade.replace(' ','\ ')).split()
      late = isLate(splitted)
      wrongFileName = True

  #grading time!
  if not fileToGrade == "" and late < 3:
    answers = []
    for x in range(0,len(inputArray),3):
      process = subprocess.Popen(['python3', fileToGrade], stdin = subprocess.PIPE, stdout = subprocess.PIPE)
      out = process.communicate(bytes(inputArray[x] + '\n' + inputArray[x+1] + '\n' + inputArray[x+2], 'UTF-8'))[0]
      answers.append(str(out)[2:-1])
     
    perfectCount = 0
    closeCount = 0 
    answerCount = 0
    wrongCount = 0
    for correctAnswer in correct.splitlines():
      if correctAnswer in answers[answerCount]:  #Does not contain the correct formatted answer
        print('Correct answer for #', answerCount+1)
        perfectCount += 1
      else:
        correctSplitted = correctAnswer.split()
        if  correctSplitted[-1][:-1].lower() in answers[answerCount].lower() :
          print ("Correct answer for #", answerCount+1," but incorrect formatting")
          print ("\t", correctAnswer," vs. ",answers[answerCount])
          closeCount += 1
        else:
          print("Wrong answer")
          print ("\t", correctAnswer," vs. ",answers[answerCount])
          wrongCount += 1
      answerCount += 1
    print("Perfect:", str(perfectCount) + "/10")
    print("Close:", str(closeCount) + "/10")
    print("Wrong:", str(wrongCount) + "/10")
    if wrongCount != 0 or closeCount != 0:
        grade -= (2 * wrongCount) 
        grade -= (1 * closeCount)
        comments += " Output did not match instructors, "


  #checking for header and style
  #os.system('vim ' + fileToGrade)
  input("Hit Enter to cat")
  print(subprocess.getoutput('cat ' + fileToGrade))
  headerInput = input("Header( (y or enter)  /  n)? ")
  if headerInput == 'y' or headerInput == '' :
    header = True
  else :
    header = False
  style = input("Style/Comments (Enter a number out of 30 to represent their grade, hit enter for 30): ")
  comments += input ("General Comments?:  ")
  if not style.isdigit() :
    style = 30
  else :
    style = int(style)
  
  #writing grade time!
  if late == -1:
    if writeToFile: outputFile.write('0\t More than 7 days late')
  else :
    if late == 3:
      comments = "3 - 7 days late, "
      grade -= 30
    elif late == 2 :
      comments = "2 days late, "
      grade -= 20
    elif late == 1 :
      comments = "1 day late, "
      grade -= 10
    
    if wrongFileName :
      comments += " wrong filename, "
      grade -= 10
    if not header :
      comments += " no/malformed header, "
      grade -= 10

    if writeToFile: outputFile.write(str(grade+style) + "\t"+comments.rstrip(', '))
      
  if writeToFile: outputFile.write('\n')
  os.chdir("..")
Exemplo n.º 25
0
def assign2( csid , writeToFile) :
  fileToGrade = ""
  late = 0
  grade = 70
  style = 30
  wrongFileName = False
  header = True
  comments = " "
  manualComments = ""

  os.chdir(csid)
  if writeToFile: outputFile.write(csid + "\t")
  files = os.listdir('.')

  #filename checking
  for f in files :
    splitted = subprocess.getoutput('ls -l ' + f).split()
    if f == filename :
      fileToGrade = filename
      late = isLate(splitted)
      break
    elif f == filename.lower() :
      fileToGrade = filename.lower()
      late = isLate(splitted)
      wrongFileName = True
      break

  #really odd filename
  if fileToGrade == "" :
    print(subprocess.getoutput('ls -l'))
    fileToGrade = input("Which file should I grade? ")
    if fileToGrade == "" :
      if writeToFile: 
        outputFile.write("0\tno file\n")
        os.chdir("..")
        return
    else :
      splitted = subprocess.getoutput('ls -l ' + fileToGrade.replace(' ','\ ')).split()
      late = isLate(splitted)
      wrongFileName = True

  #grading time!
  if not fileToGrade == "" and late < 3:
    answers = []
    for creditCardNumber in inputArray:
      process = subprocess.Popen(['python3', fileToGrade], stdin = subprocess.PIPE, stdout = subprocess.PIPE)
      out = process.communicate(bytes(creditCardNumber, 'UTF-8'))[0]
      answers.append(str(out)[2:-1])
     
    perfectCount = 0
    closeCount = 0
    wrongCount = 0
    VISA_answers = [0, 1, 2, 3]
    MasterCard_answers = [4, 5]
    correct_VISA_count = 0
    correct_MasterCard_count = 0
    # Used to ensure the inverse of the right answer is not also in the output.
    inverse = {'Valid credit card number': 'Invalid credit card number',
               'Invalid credit card number': 'Valid credit card number'}
    correct_answers = correct.splitlines()
    for answerCount, correctAnswer in enumerate(correct_answers):
      answer = answers[answerCount]
      # Contains the correct formatted answer.
      if correctAnswer in answer and not inverse[correctAnswer] in answer:  
        print('Correct answer for #', answerCount+1)
        perfectCount += 1
        # If the card number is a VISA, check if they did extra credit.
        if answerCount in VISA_answers and '\\nVISA\\n' in answer:
            correct_VISA_count += 1
        # If the card number is a MasterCard, check if they did extra credit.
        if answerCount in MasterCard_answers and '\\nMasterCard\\n' in answer:
            correct_MasterCard_count += 1
      # Contains right answer, but not correctly formatted.
      elif correctAnswer.lower()[0:5] in answer.lower() and not inverse[correctAnswer].lower()[0:5] in answer.lower():
        print ("Correct answer for #", answerCount+1," but incorrect formatting")
        print ("\t", correctAnswer[0:1],"-",answer)
        closeCount += 1
      else:
        print("Wrong answer for #", answerCount+1)
        wrongCount += 1
    print("Perfect:", str(perfectCount) + "/" + str(len(correct_answers)))
    print("Close:", str(closeCount) + "/" + str(len(correct_answers)))
    print("Wrong:", str(wrongCount) + "/" + str(len(correct_answers)))
    print("Correct VISA:", str(correct_VISA_count) + "/" + str(len(VISA_answers)))
    print("Correct MasterCard:", str(correct_MasterCard_count) + "/" + str(len(MasterCard_answers)))
    grade = 70 - (5 * wrongCount) 
    if(1 <= closeCount <= 4):
      grade -= 5
      comments += " Output did not match instructor's, "
    elif(closeCount > 4):
      grade -= 10
      comments += " Output did not match instructor's, "
    # Add points for extra credit - 5 if all correct, 3 if some.
    if correct_VISA_count == len(VISA_answers) and correct_MasterCard_count == len(MasterCard_answers):
      grade += 5
    elif correct_VISA_count > 0 or correct_MasterCard_count > 0:
      grade += 3


  #TODO fix the else below to output the correct comments   
  else:
    #print('Their output:')
    #print(out)
    #print('Correct output:')
    #print(correct)
    #print('logo has ' + str(len(lines)) +' lines, not 19')
    #don't dock points for lateness or wrong filename here
    gradeInput = input("Grade out of 70 (no style, hit enter if 65): ")
    if gradeInput == '' :
      grade = 65
    else :
      grade = int(gradeInput)
    manualComments = input("Comments: ")

  #checking for header and style
  #os.system('vim ' + fileToGrade)
  input("Hit Enter to cat")
  print(subprocess.getoutput('cat ' + fileToGrade))
  headerInput = input("Header( (y or enter)  /  n)? ")
  if headerInput == 'y' or headerInput == '' :
    header = True
  else :
    header = False
  style = input("Style/Comments (Enter a number out of 30 to represent their grade, hit enter for 30): ")
  comments += input ("General Comments?:  ")
  if not style.isdigit() :
    style = 30
  else :
    style = int(style)
  
  #writing grade time!
  if late == 3 :
    if writeToFile: outputFile.write('0\t 3 days late')
  else :
    if late == 2 :
      comments = "2 days late, "
      grade -= 20
    elif late == 1 :
      comments = "1 day late, "
      grade -= 10
    
    if wrongFileName :
      comments += " wrong filename, "
      grade -= 10
    if not header :
      comments += " no/malformed header, "
      grade -= 10
    if writeToFile: outputFile.write(str(grade+style) + "\t"+comments.rstrip(', ') + manualComments)
      
  if writeToFile: outputFile.write('\n')
  os.chdir("..")
def assign05(csid, writeToFile) :
  fileToGrade = ""
  late = 0
  grade = 70
  style = 30
  wrongFileName = False
  header = True
  comments = []

  os.chdir(csid)
  if writeToFile: outputFile.write(csid + "\t")
  files = os.listdir('.')

  #filename checking
  for f in files :
    splitted = subprocess.getoutput('ls -l ' + f).split()
    if f == filename :
      fileToGrade = filename
      late = isLate(splitted)
      break
    elif f == filename.lower() :
      fileToGrade = filename.lower()
      late = isLate(splitted)
      wrongFileName = True
      break

  #really odd filename
  if fileToGrade == "" :
    print(subprocess.getoutput('ls -l'))
    fileToGrade = input("Which file should I grade? ")
    if fileToGrade == "" :
      if writeToFile:
        outputFile.write("0\tno file\n")
      os.chdir("..")
      return
    else :
      splitted = subprocess.getoutput('ls -l ' + fileToGrade.replace(' ','\ ')).split()
      late = isLate(splitted)
      wrongFileName = True

  #grading time!
  '''
  6 players - 10 pts each
  1 dealer - 10 pts
  '''

  inputText = [6]
  outputList = []
  formatted = False
  crashList = []

  if not (fileToGrade == '' and late != -1):
    for numPlayers in inputText:
      try:
        process = subprocess.Popen(['python3', fileToGrade], **pipes)
        out = process.communicate(bytes(str(numPlayers), 'UTF-8'))[0]
        


        outputGame = str(out)[2:-1].split('\\n')
        


        crashed = False

        for o in outputGame:
          if o.find('Traceback') > -1:
            crashed = True
            break
        crashList.append(crashed)

        if not crashed:
          # check output format of all 11 tests
          if (outputGame[0].find('Enter number of players: ') < 0) or (outputGame[0].find('Enter the number of players: ') < 0):
            formatted = True

        outputList.append(list(filter(None, outputGame)))
        
      except KeyboardInterrupt:
        print(' passed ^C')

  grade = 0

  # # run tests
  # for count in range(len(outputList)):
  #   output = outputList[count]
  #   numPlayers = inputText[count]
  #   hands = []
  #   if not crashList[count]:
  #     for test in output:
  #       hand = []
  #       for token in test.split():
  #         if isCard(token):
  #           hand.append(card(token))
  #       if len(hand) == 5:
  #         hands.append(hand)
  #     if len(hands) != numPlayers:
  #       comments.append('failed test ' + str(count + 1) + ' (-5) - Wrong Number of Hands')
  #       print('Failed Test ' + str(count + 1) + ':')
  #       print('\tWrong Number of Hands')
  #     else:
  #       values = []
  #       failed = False

  #       if not hasDuplicate:
  #         hasDuplicate, hand = checkDuplicate(hands)
  #         if hasDuplicate:
  #             hasDuplicateList.append(hand)

  #       for i in range(len(hands)):
  #         hand = hands[i]
  #         sortedHand1 = sorted(hand, reverse=True)
  #         h1, sortedHand2 = is10(sortedHand1)
  #         h2, sortedHand3 = isStraight(sortedHand1)

  #         if isSorted:
  #           isSorted = sameHand(hand, sortedHand1) or sameHand(hand, sortedHand2) or sameHand(hand, sortedHand3)
  #           if not isSorted:
  #             isSortedList.append(printHand(hand))

  #         if not failed:
  #           indType = numPlayers
  #           while (indType < len(output)) and (output[indType].find('1:') < 0):
  #             indType += 1

  #           j = indType + i
  #           line = output[j].rstrip() if j < len(output) else ''
  #           pos = line.find(str(i + 1))

  #           if pos >= 0:
  #             for p in range(pos, len(line)):
  #               if 'a' <= line[p].lower() <= 'z':
  #                 pos = p
  #                 break
  #             line = line[pos:].lower()

  #             v = -1
  #             if line in mapping:
  #               v = mapping[line]

  #             if (v == -1) or ((v != h1) and (v != h2)):
  #               comm = 'Hand = ' + printHand(hand) + '; Output = ' + line
  #               comments.append('failed test ' + str(count + 1) + ' (-5) ' + comm)
  #               print('Failed Test ' + str(count + 1) + ':')
  #               print('\tHand: ' + printHand(hand))
  #               print('\tType: ' + line)
  #               failed = True
  #               break

  #             if not failed:
  #               if v == h1:
  #                 values.append(computeScore(h1, sortedHand2))
  #               else:
  #                 values.append(computeScore(h2, sortedHand3))

  #           else:
  #             comments.append('failed test ' + str(count + 1) + ' (-5) - Unable to Find Type')
  #             print('Failed Test ' + str(count + 1) + ':')
  #             print('\tHand: ' + printHand(hand))
  #             print('\tType: ' + line)
  #             failed = True
  #             break

  #       if not failed:
  #         if len(values) != 0:
  #           maxScore = max(values)

  #           winners = [i + 1 for i in range(len(values)) if values[i] == maxScore][::-1]

  #           if len(winners) == 1:
  #             if output[-1].find(str(winners[0])) < 0:
  #               strList = map(lambda x: printHand(x), hands)
  #               comm = 'Hands = [' + ', '.join(strList) + ']; Winner = ' + str(winners[0]) + '; Output = ' + output[-1]
  #               comments.append('failed test ' + str(count + 1) + ' (-5) ' + comm)
  #               print('Failed Test ' + str(count + 1) + ':')
  #               print('\tCorrect: ' + str(winners[0]))
  #               print('\tOutput: ' + output[-1])
  #               failed = True

  #           else:
  #             pos = -1
  #             for w in winners:
  #               if output[pos].find(str(w)) < 0:
  #                 strList = map(lambda x: printHand(x), hands)
  #                 comm = 'Hands = [' + ', '.join(strList) + ']; Winner = ' + str(w) + '; Output = ' + output[pos]
  #                 comments.append('failed test ' + str(count + 1) + ' (-5) ' + comm)
  #                 print('Failed Test ' + str(count + 1) + ':')
  #                 print('\tCorrect: ' + str(w))
  #                 print('\tOutput: ' + output[pos])
  #                 failed = True
  #                 break

  #       if not failed:
  #         grade += 5

  # if formatted:
  #   grade += 5
  # else:
  #   comments.append('wrong output format (-5)')

  # if isSorted:
  #   grade += 5
  # else:
  #   comm = '[' + ', '.join(isSortedList) + ']'
  #   comments.append('spotted incorrectly sorted hand (-5) ' + comm)

  # if not hasDuplicate:
  #   grade += 5
  # else:
  #   comm = 'Hands = [' + ', '.join(hasDuplicateList) + ']'
  #   comments.append('spotted duplicate cards (-5) ' + comm)

  # if grade == 70:
  #   print('Perfection =D')
  # else:
  #   print('Grade: ' + str(grade)+'/70')


  #checking for header and style
  input("Hit Enter to cat first 20 lines (header)")
  print(subprocess.getoutput('head -20 ' + fileToGrade))
  headerInput = input("Header(y/n, hit enter for y): ")
  if headerInput == 'y' or headerInput == '':
    header = True
  else :
    header = False
  input("Hit Enter to cat whole file (style/comments)")
  print(subprocess.getoutput('cat ' + fileToGrade))
  style = input("Style/Other (Out of 30, hit enter for 30): ")
  gen_comments = input("General Comments?: ").rstrip().lstrip()
  gen_comments = gen_comments if len(gen_comments) is not 0 else "style"
  if not style.isdigit():
    style = 30
  else :
    style = int(style)
  if (gen_comments != "style" or style != 30):
    gen_comments += " (%+d)" % (style - 30)
    comments.append("%s" % gen_comments)

  #writing grade time!
  if late == -1:
    if writeToFile: outputFile.write('0\t More than 7 days late')
    print('Late more than 7 days!')
  else :
    if late == 3:
      comments.append("3-7 days late (-30)")
      grade -= 30
    elif late == 2:
      comments.append("2 days late (-20)")
      grade -= 20
    elif late == 1:
      comments.append("1 day late (-10)")
      grade -= 10

    if wrongFileName or not header:
      grade -= 5
      if wrongFileName and header:
        comments.append("wrong filename (-5)")
      elif header and not wrongFileName:
        comments.append("malformed header (-5)")
      else:
        comments.append("wrong filename and malformed header (-5)")

    if writeToFile: outputFile.write(str(grade+style) + "\t" + ', '.join(comments))

  if writeToFile: outputFile.write('\n')
  os.chdir("..")
def assign12(csid, writeToFile,editor):
  fileToGrade = ""
  late = 0
  grade = 70
  style = 30
  wrongFileName = False
  header = True
  comments = []
  
  os.chdir(csid)
  if writeToFile: outputFile.write(csid + "\t")
  files = os.listdir('.')

  #filename checking
  for f in files :
    splitted = subprocess.getoutput('ls -l ' + f).split()
    if f == filename :
      fileToGrade = filename
      late = isLate(splitted)
      break
    elif f == filename.lower() :
      fileToGrade = filename.lower()
      late = isLate(splitted)
      wrongFileName = True
      break

  #really odd filename
  if fileToGrade == "" :
    print(subprocess.getoutput('ls -l'))
    fileToGrade = input("Which file should I grade? ")
    if fileToGrade == "" :
      if writeToFile:
        outputFile.write("0\tno file\n")
      os.chdir("..")
      return
    else :
      splitted = subprocess.getoutput('ls -l ' + fileToGrade.replace(' ','\ ')).split()
      late = isLate(splitted)
      wrongFileName = True

  #grading time!
  '''
  70 + 10
  
  15 - Sparse add()
  15 - Sparse mult()
  10 - Sparse getRow()
  10 - Sparse getCol()
  20 - Sparse str()
    10 - formatting
    10 - including 0's
  setElement broken = -10

  +10 - Sparse str() extra credit
  '''
  grade = 70

  #Test these squares
  formatting = True
  if not (fileToGrade == '' and late != -1):
    #grab output
    output = []
    for run in range(1,4):
      try:
        print("\n=====File %d===== (Ours then Theirs)"%run)
        testFile = 'matrix%d.txt'%run
        outFile = 'out%d.txt'%run
        correctFile = '../correct%d.txt'%run
        os.system('cp ../'+ testFile +' matrix.txt')
        output.append(subprocess.getoutput('python3 ' + fileToGrade + '> ' + outFile).splitlines())

        #modifying output
        f = open(outFile,'r')
        lines = f.readlines()
        f.close()
        f = open(outFile,'w')
        lines = [x.rstrip() for x in lines]
        lines = list(filter(None,lines))
        f.write('\n'.join(lines))
        f.close()

        os.system('diff -yB ' + correctFile +' '+outFile)
        print('\n'+'='*35)
      except KeyboardInterrupt:
        print(' passed on run',run)

      off = input('points off: ')
      while off != '':
        grade -= int(off)
        print('Current Grade: %d'%grade)
        comment = input('comments? ')
        if comment != '':
          comments.append(comment + ' (-%s)'%off)
        off = input('points off: ')
    print()

  if grade >= 70:
    print("<('.')^ Perfection ^('.')>")      
    print("Grade: %d/70"%grade)      
  else:
    print("Grade: %d/70"%grade)      

  #checking for header and style
  input("Hit Enter to cat first 20 lines (header)")
  print(subprocess.getoutput('head -20 ' + fileToGrade))
  headerInput = input("Header(y/n, hit enter for y): ")
  if headerInput == 'y' or headerInput == '':
    header = True
  else:
    header = False
  input("Hit Enter to cat whole file (style/comments)")
  if editor != '':
    os.system(editor +' '+ fileToGrade)
  else:
    print(subprocess.getoutput('cat ' + fileToGrade))
  style = input("Style/Other (Out of 30, hit enter for 30): ")
  gen_comments = input("General Comments?: ").strip()
  gen_comments = gen_comments if len(gen_comments) is not 0 else "style"
  if not style.isdigit():
    style = 30
  else :
    style = int(style)
  if (gen_comments != "style" or style != 30):
    gen_comments += " (%+d)" % (style - 30)
    comments.append("%s" % gen_comments)

  #writing grade time!
  if late == -1:
    if writeToFile: outputFile.write('0\t More than 7 days late')
    print('Late more than 7 days!')
  else :
    if late == 3:
      comments.append("3-7 days late (-30)")
      grade -= 30
    elif late == 2:
      comments.append("2 days late (-20)")
      grade -= 20
    elif late == 1:
      comments.append("1 day late (-10)")
      grade -= 10

    if wrongFileName or not header:
      grade -= 5
      if wrongFileName and header:
        comments.append("wrong filename (-5)")
      elif header and not wrongFileName:
        comments.append("malformed header (-5)")
      else:
        comments.append("wrong filename and malformed header (-5)")

    if writeToFile: outputFile.write(str(grade+style) + "\t" + ', '.join(comments))

  if writeToFile: outputFile.write('\n')
  os.chdir("..")
Exemplo n.º 28
0
def bootstrapLibrairies():
    if grinder:
        properties = CoreGrinder.getProperties()
        logger = grinder.logger

        # Copy all project files before loading Jar dependencies
        addProjectFiles(properties, logger)

        # A helper class to add libraries dynamically on the classloader
        cl = classPathLoader()

        ########## BOOTSTRAP CORE LIBRARIES #######
        currdir = properties.getProperty(
            'grindertool.core.lib') or '%s%s%s' % (normpath(
                os.getcwd()), os.sep, 'libs%score' % (os.sep))
        if exists(currdir):
            logger.info(
                '[agent=%d][processNumber=%d] Loading core librairies %s' %
                (grinder.agentNumber, grinder.processNumber, currdir))
            cl.addDirectory(currdir)
        else:
            logger.error(
                'Configuration error: Core libs directory not found in location %s'
                % (currdir))
            raise SyntaxError(
                'Configuration error: Core libs directory not found in location %s'
                % (currdir))

        #### OTHER LIBRARIES present under libs ####
        currdir = '%s%s%s' % (normpath(os.getcwd()), os.sep, 'libs')
        if exists(currdir):
            for a_dir in os.listdir(currdir):
                if a_dir not in ('core'):
                    logger.info(
                        '[agent=%d][processNumber=%d] Loading librairies under %s'
                        % (grinder.agentNumber, grinder.processNumber, a_dir))
                    cl.addDirectory('%s%s%s' % (currdir, os.sep, a_dir))

        ########## BOOTSTRAP SMPP ###############
        #
        # Must be load only if smpp is started
        if properties.getBoolean('grindertool.smsc.start', False):
            smpp_dir = properties.getProperty('grindertool.smsc.lib') or None
            if not smpp_dir:
                logger.error(
                    'Please set required parameter: grindertool.smsc.lib')
                raise SyntaxError(
                    'Please set required parameter: grindertool.smsc.lib')
            if not os.path.exists(smpp_dir):
                logger.error(
                    'Smpp libraries directory (grindertool.smsc.lib) %s does not exist !'
                    % (smpp_dir))
                raise SyntaxError(
                    'Smpp libraries directory (grindertool.smsc.lib) %s does not exist !'
                    % (smpp_dir))
            logger.info(
                '[agent=%d][processNumber=%d] Loading SMSC librairies %s ...' %
                (grinder.agentNumber, grinder.processNumber, smpp_dir))
            print '%s [agent=%d][processNumber=%d] Loading SMSC librairies %s ...' % (
                str(datetime.datetime.now()), grinder.agentNumber,
                grinder.processNumber, smpp_dir)
            cl = classPathLoader()
            cl.addDirectory(smpp_dir)
    # else:
    #     logger.info('No SMSC libraries loaded')
    ######################################

        if grinder.agentNumber == -1:
            print 'You are in STANDALONE mode (no console at all)'
            if properties.getBoolean('grindertool.smsc.start', False):
                print '\tSMSC logs location: %s%slog' % (''.join(
                    smpp_dir.split(os.sep)[:-2]
                    or smpp_dir.split('/')[:-2]), os.sep)
            print '\tGrindertool logs: %s' % (
                properties.getProperty('grinder.logDirectory'))
def assign07(csid, writeToFile) :
  fileToGrade = ""
  late = 0
  grade = 70
  style = 30
  wrongFileName = False
  header = True
  comments = []

  os.chdir(csid)
  if writeToFile: outputFile.write(csid + "\t")
  files = os.listdir('.')

  #filename checking
  for f in files :
    splitted = subprocess.getoutput('ls -l ' + f).split()
    if f == filename :
      fileToGrade = filename
      late = isLate(splitted)
      break
    elif f == filename.lower() :
      fileToGrade = filename.lower()
      late = isLate(splitted)
      wrongFileName = True
      break

  #really odd filename
  if fileToGrade == "" :
    print(subprocess.getoutput('ls -l'))
    fileToGrade = input("Which file should I grade? ")
    if fileToGrade == "" :
      if writeToFile:
        outputFile.write("0\tno file\n")
      os.chdir("..")
      return
    else :
      splitted = subprocess.getoutput('ls -l ' + fileToGrade.replace(' ','\ ')).split()
      late = isLate(splitted)
      wrongFileName = True

  #grading time!
  '''
  7 test cases (10 pts each) = 70 pts
  '''
  grade = 0
  inFiles = ['test1.in', 'test2.in', 'test3.in', 'test4.in', 'test5.in', 'test6.in', 'test7.in']
  outFiles = ['test1.out', 'test2.out', 'test3.out', 'test4.out', 'test5.out', 'test6.out', 'test7.out']

  for idx in range(len(inFiles)):
    print('Test ' + str(idx + 1) + ':')

    os.system('cp ../' + inFiles[idx] + ' Boxes.txt')
    os.system('cp ../' + inFiles[idx] + ' boxes.txt')
    # output = subprocess.getoutput('python3 ' + fileToGrade).split('\n\n')
    signal, output = execute(['python3', fileToGrade], 15)
    output = output.split('\n\n')
    correct = open('../' + outFiles[idx]).read().split('\n\n')

    subsets = set()
    correctSubsets = set()

    if signal != -1:
      for out in output:
        boxes = set()
        for line in out.split('\n'):
          box = set()
          for d in re.sub('[^\d]', ' ', line).split(' '):
            if d.isdigit():
              box.add(int(d))
          if len(box) == 3:
            boxes.add(frozenset(box))
        if len(boxes) > 0:
          subsets.add(frozenset(boxes))

      for cor in correct:
        boxes = set()
        for line in cor.split('\n'):
          box = set()
          for d in re.sub('[^\d]', ' ', line).split(' '):
            if d.isdigit():
              box.add(int(d))
          if len(box) == 3:
            boxes.add(frozenset(box))
        if len(boxes) > 0:
          correctSubsets.add(frozenset(boxes))

      if (subsets == correctSubsets):
        print('\tPassed')
        grade += 10
      else:
        print('\tFailed (-10)')
        comments.append('Failed Test ' + str(idx + 1))
    else:
      print('\tTimed Out (-10)')
      comments.append('Failed Test ' + str(idx + 1) + ' (Timed Out)')

  if grade == 70:
    print('Perfection =D')
  else:
    print('Grade: ' + str(grade) + '/70')

  #checking for header and style
  input("Hit Enter to cat first 20 lines (header)")
  print(subprocess.getoutput('head -20 ' + fileToGrade))
  headerInput = input("Header(y/n, hit enter for y): ")
  if headerInput == 'y' or headerInput == '':
    header = True
  else :
    header = False
  input("Hit Enter to cat whole file (style/comments)")
  print(subprocess.getoutput('cat ' + fileToGrade))
  style = input("Style/Other (Out of 30, hit enter for 30): ")
  gen_comments = input("General Comments?: ").rstrip().lstrip()
  gen_comments = gen_comments if len(gen_comments) is not 0 else "style"
  if not style.isdigit():
    style = 30
  else :
    style = int(style)
  if (gen_comments != "style" or style != 30):
    gen_comments += " (%+d)" % (style - 30)
    comments.append("%s" % gen_comments)

  #writing grade time!
  if late == -1:
    if writeToFile: outputFile.write('0\t More than 7 days late')
    print('Late more than 7 days!')
  else :
    if late == 3:
      comments.append("3-7 days late (-30)")
      grade -= 30
    elif late == 2:
      comments.append("2 days late (-20)")
      grade -= 20
    elif late == 1:
      comments.append("1 day late (-10)")
      grade -= 10

    if wrongFileName or not header:
      grade -= 5
      if wrongFileName and header:
        comments.append("wrong filename (-5)")
      elif not header and not wrongFileName:
        comments.append("malformed header (-5)")
      else:
        comments.append("wrong filename and malformed header (-5)")

    if writeToFile: outputFile.write(str(grade+style) + "\t" + ', '.join(comments))

  if writeToFile: outputFile.write('\n')
  os.chdir("..")
Exemplo n.º 30
0
def assign6( csid , writeToFile) :
  fileToGrade = ""
  late = 0
  grade = 70
  style = 30
  wrongFileName = False
  header = True
  comments = " "

  os.chdir(csid)
  if writeToFile: outputFile.write(csid + "\t")
  files = os.listdir('.')

  #filename checking
  for f in files :
    splitted = subprocess.getoutput('ls -l ' + f).split()
    if f == filename :
      fileToGrade = filename
      late = isLate(splitted)
      break
    elif f == filename.lower() :
      fileToGrade = filename.lower()
      late = isLate(splitted)
      wrongFileName = True
      break

  #really odd filename
  if fileToGrade == "" :
    print(subprocess.getoutput('ls -l'))
    fileToGrade = input("Which file should I grade? ")
    if fileToGrade == "" :
      if writeToFile: 
        outputFile.write("0\tno file\n")
      os.chdir("..")
      return
    else :
      splitted = subprocess.getoutput('ls -l ' + fileToGrade.replace(' ','\ ')).split()
      late = isLate(splitted)
      wrongFileName = True

  #grading time!
  if not fileToGrade == "" and late != -1:
    out = "XXInterrputed sprobs will be set to 13X"
    process = subprocess.Popen(['python3', fileToGrade], stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
    try:
      out = process.communicate(bytes('50000', 'UTF-8'))[0]
    except KeyboardInterrupt:
      pass
    #yup this next line of code is a little funky but yeah... it works and I'm lazy --Devin
    perfect = True
    wrong = False
    answer = (str(out)[2:-1]).replace('\\n','\n').rstrip().split('\n')
    try:
      switch = int(re.findall("\D+\d+\.(\d+)",answer[-2])[0])
      noSwitch = int(re.findall("\D+\d+\.(\d+)",answer[-1])[0])
    except IndexError:
      wrong = True
      perfect = False
      switch = 1337
      noSwitch = 1337
    #penalize for not rounding
    if switch > 99 or noSwitch > 99:
      perfect = False
      switch = round(int(str(switch)[:3]), -1) // 10
      noSwitch = round(int(str(noSwitch)[:3]), -1) // 10
    if perfect and switch + noSwitch == 100 and ( 66 <= switch <= 68) and (32 <= noSwitch <= 34):
      pass
      print("Perfect (feels nice to be nice =D)")
    elif not wrong and switch + noSwitch == 100 and (( 66 <= switch <= 68) or (32 <= noSwitch <= 34)):
      grade -= 10
      print("Close")
      print(answer[-2:])
      print("Prob when switching: " + str(switch) + " Prob when not switching: " +str(noSwitch))
      comments += "Somewhat wrong output!"
    else:
      grade -= 30
      print("Wrong")
      print(answer[-2:])
      print("Prob when switching: " + str(switch) + " Prob when not switching: " +str(noSwitch))
      comments += "Wrong output!"
  #checking for header and style
  #os.system('vim ' + fileToGrade)
  input("Hit Enter to cat")
  print(subprocess.getoutput('cat ' + fileToGrade))
  headerInput = input("Header( (y or enter)  /  n)? ")
  if headerInput == 'y' or headerInput == '' :
    header = True
  else :
    header = False
  style = input("Style/Comments (Enter a number out of 30 to represent their grade, hit enter for 30): ")
  comments += input ("General Comments?:  ")
  if not style.isdigit() :
    style = 30
  else :
    style = int(style)
  
  #writing grade time!
  if late == -1:
    if writeToFile: outputFile.write('0\t More than 7 days late')
    print('Late more than 7 days!')
  else :
    if late == 3:
      comments = "3 - 7 days late, "
      grade -= 30
    elif late == 2 :
      comments = "2 days late, "
      grade -= 20
    elif late == 1 :
      comments = "1 day late, "
      grade -= 10
    
    if wrongFileName :
      comments += " wrong filename, "
      grade -= 10
    if not header :
      comments += " no/malformed header, "
      grade -= 10

    if writeToFile: outputFile.write(str(grade+style) + "\t"+comments.rstrip(', '))
      
  if writeToFile: outputFile.write('\n')
  os.chdir("..")
def assign01(csid, writeToFile) :
  fileToGrade = ""
  late = 0
  grade = 70
  style = 30
  wrongFileName = False
  header = True
  comments = []

  os.chdir(csid)
  if writeToFile: outputFile.write(csid + "\t")
  files = os.listdir('.')

  #filename checking
  for f in files :
    splitted = subprocess.getoutput('ls -l ' + f).split()
    if f == filename :
      fileToGrade = filename
      late = isLate(splitted)
      break
    elif f == filename.lower() :
      fileToGrade = filename.lower()
      late = isLate(splitted)
      wrongFileName = True
      break

  #really odd filename
  if fileToGrade == "" :
    print(subprocess.getoutput('ls -l'))
    fileToGrade = input("Which file should I grade? ")
    if fileToGrade == "" :
      if writeToFile:
        outputFile.write("0\tno file\n")
      os.chdir("..")
      return
    else :
      splitted = subprocess.getoutput('ls -l ' + fileToGrade.replace(' ','\ ')).split()
      late = isLate(splitted)
      wrongFileName = True

  #grading time!
  '''
  53 points
  2 24 point runs
  substution encode/decode is 10 points
  vigenere encode/decode is 14 points
  5 for formatting
  '''

  fillerText = ['Substitution Cipher','Enter Plain Text to be Encoded: Encoded Text: ','Enter Encoded Text to be Decoded: Decoded Plain Text: ','Vigenere Cipher','Enter Plain Text to be Encoded: Enter Pass Phrase (no spaces allowed): Encoded Text: ','Enter Encoded Text to be Decoded: Enter Pass Phrase (no spaces allowed): Decoded Plain Text: ']
  inputText = []
  outputText = []
  outputText.append(['dsvvb kbnvw','hello world','zilwg aocdh','hello world'])
  outputText.append(['dqc emlp!!!','hai   guyz','qfh','!!tri  cky!!'])
  inputText.append(['hello world', 'dsvvb kbnvw','hello world','seal','zilwg aocdh','seal'])
  inputText.append(['HAI GUYZ!!!', 'dqc   emlp','xyz','thisisapassphrase','!!tri  cky!!','aaaaaazzzzzz'])

  if not (fileToGrade == '' and late != -1):
    correctFormatting = True
    count = 0
    for (inText,outText) in zip(inputText,outputText):
      try:
        process = subprocess.Popen(['python3',fileToGrade], **pipes)
        out = process.communicate(bytes('\n'.join(inText), 'UTF-8'))[0]
        answers = list(filter(None, str(out)[2:-1].split('\\n')))
      except KeyboardInterrupt:
        print(' passed ^C')

      #there's probably a way to loop through this jazz but meh
      correctAnswers = []
      correctAnswers.append(fillerText[0])
      correctAnswers.append(fillerText[1] + outText[0])
      correctAnswers.append(fillerText[2] + outText[1])
      correctAnswers.append(fillerText[3])
      correctAnswers.append(fillerText[4] + outText[2])
      correctAnswers.append(fillerText[5] + outText[3])
      modCount = count % 6

      for (theirs, ours) in zip(answers,correctAnswers):
        if correctFormatting:
          if theirs.strip() != ours.strip():
            correctFormatting = False
            grade -= 5
            comments.append('Incorrect Formatting (-5) yours:' + theirs +' ours:'+ours)
            print('\tIncorrect Formatting')
            print('\tTheirs\n\t'+theirs)
            print('\tOurs\n\t'+ours)
          else:
            print('Passed Test ' + str(count + 1))
        if not correctFormatting and modCount != 0 and modCount != 3:
          t = theirs.split(':')[-1].strip() 
          o = ours.split(':')[-1].strip()
          if t != o: 
            comments.append('Failed Test '+ str(count+1))
            print('X '+comments[-1])
            print('\tTheirs\n\t'+theirs)
            print('\tOurs\n\t'+ours)
            if modCount == 1 or modCount == 2:
              grade -= 5
            else:
              grade -= 7
          else:
            print('Passed Test ' + str(count+1))
        count += 1
        modCount += 1
  if grade == 70:
    print('Perfection =D')
  else:
    print('Grade: ' + str(grade)+'/70')

  #checking for header and style
  input("Hit Enter to cat")
  print(subprocess.getoutput('cat ' + fileToGrade))
  headerInput = input("Header(y/n, hit enter for y): ")
  if headerInput == 'y' or headerInput == '':
    header = True
  else:
    header = False
  style = input("Style/Other (Out of 30, hit enter for 30): ")
  gen_comments = input("General Comments?: ").rstrip().lstrip()
  gen_comments = gen_comments if len(gen_comments) is not 0 else "style"
  if not style.isdigit():
    style = 30
  else :
    style = int(style)
  if (gen_comments != "style" or style != 30):
    gen_comments += " (%+d)" % (style - 30)
    comments.append("%s" % gen_comments)

  #writing grade time!
  if late == -1:
    if writeToFile: outputFile.write('0\t More than 2 days late')
    print('Late more than 2 days!')
  else:
    if late == 2:
      comments.append("2 days late (-20)")
      grade -= 20
    elif late == 1:
      comments.append("1 day late (-10)")
      grade -= 10

    if wrongFileName or not header:
      grade -= 5
      if wrongFileName:
        comments.append("wrong filename (-5)")
      elif header:
        comments.append("malformed header (-5)")
      else:
        comments.append("wrong filename and malformed header (-5)")

  if writeToFile: outputFile.write(str(grade+style) + "\t" + ', '.join(comments))

  if writeToFile: outputFile.write('\n')
  os.chdir("..")
def assign09(csid, writeToFile) :
  fileToGrade = ""
  late = 0
  grade = 70
  style = 30
  wrongFileName = False
  header = True
  comments = []

  os.chdir(csid)
  if writeToFile: outputFile.write(csid + "\t")
  files = os.listdir('.')

  #filename checking
  for f in files :
    splitted = subprocess.getoutput('ls -l ' + f).split()
    if f == filename :
      fileToGrade = filename
      late = isLate(splitted)
      break
    elif f == filename.lower() :
      fileToGrade = filename.lower()
      late = isLate(splitted)
      wrongFileName = True
      break

  #really odd filename
  if fileToGrade == "" :
    print(subprocess.getoutput('ls -l'))
    fileToGrade = input("Which file should I grade? ")
    if fileToGrade == "" :
      if writeToFile:
        outputFile.write("0\tno file\n")
      os.chdir("..")
      return
    else :
      splitted = subprocess.getoutput('ls -l ' + fileToGrade.replace(' ','\ ')).split()
      late = isLate(splitted)
      wrongFileName = True

  #grading time!
  '''
  30: for 3 x 3 squares
    15: unique squares
    15: correct number for squares
  40: for the rest
    10: magic square of size 7
    10: magic square of size 9
    10: reprompts user for bad input, and 3x3 square
    10: formatting
  '''
  grade = 70

  #Test these squares
  inputText = ['7','9','2\n3']
  inputNums = [7,9,3]
  output = []
  formatting = True
  if not (fileToGrade == '' and late != -1):
    #grab output
    for text in inputText:
      try:
        process = subprocess.Popen(['python3', fileToGrade], **pipes)
        out = process.communicate(bytes(text, 'UTF-8'))[0]
        output.append(str(out)[2:-1].split('\\n'))
      except KeyboardInterrupt:
        print(' passed ^C on input',text)

    #test output
    checkedPermutations = False
    for (out,test) in zip(output,inputNums):
      out = [x.rstrip() for x in out]
      out = list(filter(None,out))

      filtered = []
      for line in out:
        #people spelt square wrong...
        if 'square' not in line and 'odd' not in line and 'magic' not in line:
          filtered.append(line)
      if len(filtered) != test + (3*8):
        print()

      square = [filtered[y] for y in range(test)]
      if formatting and not checkFormatting(square):
        formatting = False
        grade -= 10
        comment = "incorrectly formatted square (-10)"
        print(comment)
        for row in square:
          print('\t' + str(row))
        comments.append(comment)
      if not checkMagicSquare(square):
        grade -= 10
        comment = "%dx%d magic square isn't well formed (-10)"%(test,test)
        if test == 3:
          comment += ', might not have reprompted for input when given an even number'
        print(comment)
        for row in square:
          print('\t' + str(row))
        comments.append(comment)

      if not checkedPermutations: #only check permutations once
        checkedPermutations = True
        permutations = []
        trimmed = filtered[test:]
        for x in range(0,len(trimmed),3):
          square = []
          square.append(trimmed[x])
          if x + 1 < len(trimmed):
            square.append(trimmed[x+1])
          if x + 2 < len(trimmed):
            square.append(trimmed[x+2])
          permutations.append(square)
        if len(permutations) != 8:
          grade -= 15
          comment = 'outputted %d 3x3 squares instead of 8 (-15)'%len(permutations)
          print(comment)
          comments.append(comment)
        if not checkPermutations(permutations):
          grade -= 15
          comment = 'incorrect set of unique 3x3 squares (-15)'
          print(comment)
          comments.append(comment)

      if len(filtered) != test + (3*8):
        s = 'more' if len(filtered) > test + (3*8) else 'less'
        print('!!!\nparsed output contains %s lines than expected'%s)
        print('dumping raw output for test %dx%d'%(test,test))
        for line in out:
          print(line)
        off = input('Points off (enter is 0 too): ')
        off = off.strip()
        if off.isdigit():
          comment = input('Comment: ')
          comments.append(comment)
          grade -= int(off)

  if grade == 70:
    print("<('.')^ Perfection ^('.')>")      
  else:
    print("Grade: %d/70"%grade)      

  #checking for header and style
  input("Hit Enter to cat first 20 lines (header)")
  print(subprocess.getoutput('head -20 ' + fileToGrade))
  headerInput = input("Header(y/n, hit enter for y): ")
  if headerInput == 'y' or headerInput == '':
    header = True
  else :
    header = False
  input("Hit Enter to cat whole file (style/comments)")
  print(subprocess.getoutput('cat ' + fileToGrade))
  style = input("Style/Other (Out of 30, hit enter for 30): ")
  gen_comments = input("General Comments?: ").rstrip().lstrip()
  gen_comments = gen_comments if len(gen_comments) is not 0 else "style"
  if not style.isdigit():
    style = 30
  else :
    style = int(style)
  if (gen_comments != "style" or style != 30):
    gen_comments += " (%+d)" % (style - 30)
    comments.append("%s" % gen_comments)

  #writing grade time!
  if late == -1:
    if writeToFile: outputFile.write('0\t More than 7 days late')
    print('Late more than 7 days!')
  else :
    if late == 3:
      comments.append("3-7 days late (-30)")
      grade -= 30
    elif late == 2:
      comments.append("2 days late (-20)")
      grade -= 20
    elif late == 1:
      comments.append("1 day late (-10)")
      grade -= 10

    if wrongFileName or not header:
      grade -= 5
      if wrongFileName and header:
        comments.append("wrong filename (-5)")
      elif header and not wrongFileName:
        comments.append("malformed header (-5)")
      else:
        comments.append("wrong filename and malformed header (-5)")

    if writeToFile: outputFile.write(str(grade+style) + "\t" + ', '.join(comments))

  if writeToFile: outputFile.write('\n')
  os.chdir("..")
def assign15(csid, writeToFile) :
  fileToGrade = ""
  late = 0
  grade = 0
  style = 30
  wrongFileName = False
  header = True
  comments = []

  os.chdir(csid)
  if writeToFile: outputFile.write(csid + "\t")
  files = os.listdir('.')

  #filename checking
  for f in files :
    splitted = subprocess.getoutput('ls -l ' + f).split()
    if f == filename :
      fileToGrade = filename
      late = isLate(splitted)
      break
    elif f == filename.lower() :
      fileToGrade = filename.lower()
      late = isLate(splitted)
      wrongFileName = True
      break

  #really odd filename
  if fileToGrade == "" :
    print(subprocess.getoutput('ls -l'))
    fileToGrade = input("Which file should I grade? ")
    if fileToGrade == "" :
      if writeToFile:
        outputFile.write("0\tno file\n")
      os.chdir("..")
      return
    else :
      splitted = subprocess.getoutput('ls -l ' + fileToGrade.replace(' ','\ ')).split()
      late = isLate(splitted)
      wrongFileName = True

  #grading time!
  '''
  5 files, 2 cases (toposort/mst, djikstras)
  10 cases total
  7 points per case
  '''

  if not (fileToGrade == '' and late != -1):
    correctFormatting = True
    count = 0
    for run in range(1,6):
      try:
        print("\n=====File %d====="%run)
        testFile = 'graph%d.txt'%run
        correctFile = '../correct%d.txt'%run
        os.system('cp ../'+ testFile +' graph.txt')
        out = subprocess.getoutput('python3 ' + fileToGrade).split('\n')
        points,c = getPoints(run,out)
        grade += points
        comments += c
        print(str(points) + '/14')
        if c != []:
          print('\n'.join(c))

      except KeyboardInterrupt:
        print(' passed on run',run)
  
  grade = 0 if grade < 0 else grade
  if grade == 70:
    print('Perfection =D')
  else:
    print('Grade: ' + str(grade)+'/70')

  #checking for header and style
  input("Hit Enter to cat first 20 lines (header)")
  print(subprocess.getoutput('head -20 ' + fileToGrade))
  headerInput = input("Header(y/n, hit enter for y): ")
  if headerInput == 'y' or headerInput == '':
    header = True
  else :
    header = False
  input("Hit Enter to cat whole file (style/comments)")
  print(subprocess.getoutput('cat ' + fileToGrade))
  style = input("Style/Other (Out of 30, hit enter for 30): ")
  gen_comments = input("General Comments?: ").rstrip().lstrip()
  gen_comments = gen_comments if len(gen_comments) is not 0 else "style"
  if not style.isdigit():
    style = 30
  else :
    style = int(style)
  if (gen_comments != "style" or style != 30):
    gen_comments += " (%+d)" % (style - 30)
    comments.append("%s" % gen_comments)

  #writing grade time!
  if late in [-1,2,3]:
    if writeToFile: outputFile.write('0\t More than 1 day late\n')
    if writeToFile: outputFile.write('\n')
    os.chdir("..")
    print('Late more than 1 day')
    return
  elif late == 1:
    comments.append("1 day late (-10)")
    grade -= 10
    print('1 day late (-10)')

  if wrongFileName or not header:
    grade -= 5
    if wrongFileName and header:
      comments.append("wrong filename (-5)")
    elif header and not wrongFileName:
      comments.append("malformed header (-5)")
    else:
      comments.append("wrong filename and malformed header (-5)")

  if writeToFile: outputFile.write(str(grade+style) + "\t" + ', '.join(comments))

  if writeToFile: outputFile.write('\n')
  os.chdir("..")
Exemplo n.º 34
0
def assign1( csid ) :
  fileToGrade = ""
  late = 0
  linesWrong = 0
  grade = 70
  style = 30
  wrongFileName = False
  header = True
  comments = ""
  manualComments = ""

  os.chdir(csid)
  outputFile.write(csid + "\t")
  files = os.listdir('.')

  for f in files :
    splitted = subprocess.getoutput('ls -l ' + f).split()
    if f == filename :
      fileToGrade = filename
      late = isLate(splitted)
      break
    elif f == filename.lower() :
      fileToGrade = filename.lower()
      late = isLate(splitted)
      wrongFileName = True
      break

  #really odd filename
  if fileToGrade == "" :
    print(subprocess.getoutput('ls -l'))
    fileToGrade = input("Which file should I grade? ")
    if fileToGrade == "" :
      outputFile.write("0\tno file")
    else :
      splitted = subprocess.getoutput('ls -l ' + fileToGrade.replace(' ','\ ')).split()
      late = isLate(splitted)
      wrongFileName = True

  #grading time!
  if not fileToGrade == "" and late < 3:
    out = subprocess.getoutput('python3 ' + fileToGrade)
    lines = out.split('\n')
    if len(lines) == 19 :
      if re.match('\s*', lines[0]) is None :
        linesWrong += 1
      if re.match('         \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\s*', lines[1]) is None :
        linesWrong += 1
      if re.match('        \*\*\*\$\$\$\$\$\$\$\$\$\$\$\$\$\$\*\*\s*', lines[2]) is None :
        linesWrong += 1
      if re.match('       \*\* \$\$         \$\$ \$ \*\*\s*', lines[3]) is None :
        linesWrong += 1
      if re.match('      \*\* \$\$           \$\$\$  \*\*\s*', lines[4]) is None :
        linesWrong += 1
      if re.match('     \*\* \$\$\$                 \*\*\s*', lines[5]) is None :
        linesWrong += 1
      if re.match('      \*\$\$\$\$\$\$\$\$\$           \*\*\s*', lines[6]) is None :
        linesWrong += 1
      if re.match('       \*\$\$\$\$\$\$\$\$\$\$\$\$\$\$    \*\*\s*', lines[7]) is None :
        linesWrong += 1
      if re.match('        \*\*\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\*\*\s*', lines[8]) is None :
        linesWrong += 1
      if re.match('         \*\*      \$\$\$\$\$\$\$\*\*\s*', lines[9]) is None :
        linesWrong += 1
      if re.match('          \*\*         \$\$\*\*\s*', lines[10]) is None :
        linesWrong += 1
      if re.match('           \*\*\$\$\$    \$\$\*\*\s*', lines[11]) is None :
        linesWrong += 1
      if re.match('            \*\$\$\$\$\$\$\$\$\*\*\s*', lines[12]) is None :
        linesWrong += 1
      if re.match('             \*\*     \*\*\s*', lines[13]) is None :
        linesWrong += 1
      if re.match('              \*\*   \*\*\s*', lines[14]) is None :
        linesWrong += 1
      if re.match('               \*\* \*\*\s*', lines[15]) is None :
        linesWrong += 1
      if re.match('                \*\*\*\s*', lines[16]) is None :
        linesWrong += 1
      if re.match('                 \*\s*', lines[17]) is None :
        linesWrong += 1
      if re.match('\s*', lines[18]) is None :
        linesWrong += 1
    else :
      print('Their output:')
      print(out)
      print('Correct output:')
      print(correct)
      print('logo has ' + str(len(lines)) +' lines, not 19')
      #don't dock points for lateness or wrong filename here
      gradeInput = input("Grade out of 70 (no style, hit enter if 65): ")
      if gradeInput == '' :
        grade = 65
      else :
        grade = int(gradeInput)
      manualComments = input("Comments: ")

    #checking for header and style
    #os.system('vim ' + fileToGrade)
    print(subprocess.getoutput('cat ' + fileToGrade))
    headerInput = input("Header(y or enter/n)? ")
    if headerInput == 'y' or headerInput == '' :
      header = True
    else :
      header = False
    style = input("Style/Comments (out of 30, hit enter for 30): ")
    if not style.isdigit() :
      style = 30
    else :
      style = int(style)
    
    #writing grade time!
    if late == 3 :
      outputFile.write('0\t 3 days late')
    else :
      if late == 2 :
        comments = "2 days late, "
        grade -= 20
      elif late == 1 :
        comments = "1 day late, "
        grade -= 10
      
      if wrongFileName :
        comments += "wrong filename, "
        grade -= 10
      if not header :
        comments += "no/malformed header, "
        grade -= 10

      if linesWrong > 0 and linesWrong < 3 : 
        comments += "improperly formed superman logo, "
        grade -= 5
      elif linesWrong > 3 :
        comments += "nonsensical superman logo, "
        grade -= 5
      outputFile.write(str(grade+style) + "\t"+comments.rstrip(', ') + manualComments)
      
  outputFile.write('\n')
  os.chdir("..")
def assign14(csid, writeToFile) :
  fileToGrade = ""
  late = 0
  grade = 70
  style = 30
  wrongFileName = False
  header = True
  comments = []

  os.chdir(csid)
  if writeToFile: outputFile.write(csid + "\t")
  files = os.listdir('.')

  #filename checking
  for f in files :
    splitted = subprocess.getoutput('ls -l ' + f).split()
    if f == filename :
      fileToGrade = filename
      late = isLate(splitted)
      break
    elif f == filename.lower() :
      fileToGrade = filename.lower()
      late = isLate(splitted)
      wrongFileName = True
      break

  #really odd filename
  if fileToGrade == "" :
    print(subprocess.getoutput('ls -l'))
    fileToGrade = input("Which file should I grade? ")
    if fileToGrade == "" :
      if writeToFile:
        outputFile.write("0\tno file\n")
      os.chdir("..")
      return
    else :
      splitted = subprocess.getoutput('ls -l ' + fileToGrade.replace(' ','\ ')).split()
      late = isLate(splitted)
      wrongFileName = True

  #grading time!
  '''
  75 points (caps at 70)
  10 7 pt runs
  5 for formatting
  '''

  #fillerText = ['Substitution Cipher','Enter Plain Text to be Encoded: Encoded Text: ','Enter Encoded Text to be Decoded: Decoded Plain Text: ','Vigenere Cipher','Enter Plain Text to be Encoded: Enter Pass Phrase (no spaces allowed): Encoded Text: ','Enter Encoded Text to be Decoded: Enter Pass Phrase (no spaces allowed): Decoded Plain Text: ']
  fillerText = ['Enter encryption key: ', 'Enter string to be encrypted: Encrypted string: ', 'Enter string to be decrypted: Decrypted string: ']
  inputText = []
  outputText = []

  #TODO: Modify this text: !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1========================

  outputText.append(['*!<!<!>!<<!*!<','meet me'])
  outputText.append(['<!<<!<><>><<<!<><>><<<!<><>>!<<<!>>!<><>>!<>>!<><>><<<!<<<>>','civic racecar'])
  outputText.append(['<><>><<!<<!<<!*!<<<!<><>><<!<<','meet me'])
  outputText.append(['>>!>>>!>>>!<!*!>>>>>>>>>>>>><','no cd'])
  outputText.append(['<<<><<!<><>><<<!<><!<<<>!<<!<<<!<><>><!<<<!<<<><!<><>>!<<<><','ttt   '])

  inputText.append(['meet me', 'meet me','*!<!<!>!<<!*!<'])
  inputText.append(['the quick brown fox jumps over the lazy dog', 'Hello World','<<<>!<><!>><!<><!<<<>!<<<!<>>!<<<><<!<<<>!<<!<<<>!<<<><<!<>>'])
  inputText.append(['the quick brown fox jumps over the lazy dog', 'meet me','<><>><<!<<!<<!*!<<<!<><>><<!<<'])
  inputText.append(['abcdefghijklm zyxwvutsrqpno', 'CDd ay','>>>>>>>>>>>>><<<<<<<<<<<!>>>>>>>>>>>>><<<<<<<<<<<>!<!>>!>>>'])
  inputText.append(['the quick brown fox jumps over the lazy dog', 'Alice N BOB','*!*!*!<<<!<<<!<<<'])

  if not (fileToGrade == '' and late != -1):
    correctFormatting = True
    count = 0
    for (inText,outText) in zip(inputText,outputText):
      try:
        process = subprocess.Popen(['python3',fileToGrade], **pipes)
        out = process.communicate(bytes('\n'.join(inText), 'UTF-8'))[0]
        answers = list(filter(None, str(out)[2:-1].split('\\n')))
      except KeyboardInterrupt:
        print(' passed ^C')

      #there's probably a way to loop through this jazz but meh
      correctAnswers = []
      correctAnswers.append(fillerText[0])
      correctAnswers.append(fillerText[1] + outText[0])
      correctAnswers.append(fillerText[2] + outText[1])

      for (theirs, ours) in zip(answers,correctAnswers):
        if correctFormatting:
          if theirs.strip() != ours.strip():
            correctFormatting = False
            grade -= 5
            comments.append('Incorrect Formatting (-5) YOURS:' + theirs +' OURS:'+ours)
            print('\tIncorrect Formatting')
            print('\tTheirs\n\t'+theirs)
            print('\tOurs\n\t'+ours)
          else:
            print('Passed Test ' + str(count + 1))
        if not correctFormatting:
          t = theirs.split(':')[-1].strip() 
          o = ours.split(':')[-1].strip()
          if t != o: 
            comments.append('  || Failed Test '+ str(count+1))
            comments.append(': ' + inText[(count%3)] + ' with key: ' + inText[0])
            comments.append(' YOURS:' + theirs +' OURS:'+ours)
            print('Failed Test '+ str(count+1))
            print(inText[(count%3)] + ' with key: ' + inText[0])
            print('\tTheirs\n\t'+theirs)
            print('\tOurs\n\t'+ours)
            grade -= 7
          else:
            print('Passed Test ' + str(count+1))
        count += 1
  if grade == 70:
    print('Perfection =D')
  elif grade < 0:
    grade = 0
    print('Grade: ' + str(grade)+'/70')
  else:
    print('Grade: ' + str(grade)+'/70')

  #checking for header and style
  input("Hit Enter to cat first 20 lines (header)")
  print(subprocess.getoutput('head -20 ' + fileToGrade))
  headerInput = input("Header(y/n, hit enter for y): ")
  if headerInput == 'y' or headerInput == '':
    header = True
  else :
    header = False
  input("Hit Enter to cat whole file (style/comments)")
  print(subprocess.getoutput('cat ' + fileToGrade))
  style = input("Style/Other (Out of 30, hit enter for 30): ")
  gen_comments = input("General Comments?: ").rstrip().lstrip()
  gen_comments = gen_comments if len(gen_comments) is not 0 else "style"
  if not style.isdigit():
    style = 30
  else :
    style = int(style)
  if (gen_comments != "style" or style != 30):
    gen_comments += " (%+d)" % (style - 30)
    comments.append("%s" % gen_comments)

  #writing grade time!
  if late == -1:
    if writeToFile: outputFile.write('0\t More than 7 days late')
    print('Late more than 7 days!')
  else :
    if late == 3:
      comments.append("3-7 days late (-30)")
      grade -= 30
    elif late == 2:
      comments.append("2 days late (-20)")
      grade -= 20
    elif late == 1:
      comments.append("1 day late (-10)")
      grade -= 10

    if wrongFileName or not header:
      grade -= 5
      if wrongFileName and header:
        comments.append("wrong filename (-5)")
      elif header and not wrongFileName:
        comments.append("malformed header (-5)")
      else:
        comments.append("wrong filename and malformed header (-5)")

    if writeToFile: outputFile.write(str(grade+style) + "\t" + ', '.join(comments))

  if writeToFile: outputFile.write('\n')
  os.chdir("..")
def assign01(csid, writeToFile) :
  fileToGrade = ""
  late = 0
  grade = 70
  style = 30
  wrongFileName = False
  header = True
  comments = []

  os.chdir(csid)
  if writeToFile: outputFile.write(csid + "\t")
  files = os.listdir('.')

  #filename checking
  for f in files :
    splitted = subprocess.getoutput('ls -l ' + f).split()
    if f == filename :
      fileToGrade = filename
      late = isLate(splitted)
      break
    elif f == filename.lower() :
      fileToGrade = filename.lower()
      late = isLate(splitted)
      wrongFileName = True
      break

  #really odd filename
  if fileToGrade == "" :
    print(subprocess.getoutput('ls -l'))
    fileToGrade = input("Which file should I grade? ")
    if fileToGrade == "" :
      if writeToFile:
        outputFile.write("0\tno file\n")
      os.chdir("..")
      return
    else :
      splitted = subprocess.getoutput('ls -l ' + fileToGrade.replace(' ','\ ')).split()
      late = isLate(splitted)
      wrongFileName = True

  #grading time!
  '''
  tests 1-5: 10 pts ea
  tests 6-9: 5 pts ea
  '''
  if not (fileToGrade == '' and late != -1):
    os.system('cp ../nim.txt .')
    outputLines = subprocess.getoutput('python3 ' + fileToGrade).splitlines()

    minLen = min(len(outputLines), len(correctLines))
    grade = 0
    index = 0
    testsFailed = []
    for i in range(minLen):
      if i < 5 and outputLines[i] == correctLines[i]:
        grade += 10
      elif i < 9 and outputLines[i] == correctLines[i]:
        grade += 5
      elif i == 4 and outputLines[i] == 'Remove 1 counters from Heap 1':
        grade += 10
      else:
        print('Failed Test',i)
        print('\tTheir Output')
        print('\t' + outputLines[i])
        print('\tCorrect Output')
        print('\t' + correctLines[i])
        testsFailed.append(i)

    if len(testsFailed) != 0:
      comments.append("Failed tests " + ' '.join(str(x) for x in testsFailed))
    else:
      print('Passed all tests =D')

  #checking for header and style
  input("Hit Enter to cat")
  print(subprocess.getoutput('cat ' + fileToGrade))
  headerInput = input("Header(y/n, hit enter for y): ")
  if headerInput == 'y' or headerInput == '':
    header = True
  else :
    header = False
  style = input("Style/Other (Out of 30, hit enter for 30): ")
  gen_comments = input("General Comments?: ").rstrip().lstrip()
  gen_comments = gen_comments if len(gen_comments) is not 0 else "style"
  if not style.isdigit():
    style = 30
  else :
    style = int(style)
  if (gen_comments != "style" or style != 30):
    gen_comments += " (%+d)" % (style - 30)
    comments.append("%s" % gen_comments)

  #writing grade time!
  if late == -1:
    if writeToFile: outputFile.write('0\t More than 2 days late')
    print('Late more than 2 days!')
  else :
    if late == 2:
      comments.append("2 days late (-20)")
      grade -= 20
    elif late == 1:
      comments.append("1 day late (-10)")
      grade -= 10

    if wrongFileName:
      comments.append("wrong filename (-10)")
      grade -= 10

    if not header:
      comments.append("missing comments or malformed header (-5)")
      grade -= 5

    if writeToFile: outputFile.write(str(grade+style) + "\t" + ', '.join(comments))

  if writeToFile: outputFile.write('\n')
  os.chdir("..")
def assign14(csid , writeToFile) :
  fileToGrade = ""
  late = 0
  grade = 70
  style = 30
  wrongFileName = False
  header = True
  comments = []

  os.chdir(csid)
  if writeToFile: outputFile.write(csid + "\t")
  files = os.listdir('.')

  #filename checking
  for f in files :
    splitted = subprocess.getoutput('ls -l ' + f).split()
    if f == filename :
      fileToGrade = filename
      late = isLate(splitted)
      break
    elif f == filename.lower() :
      fileToGrade = filename.lower()
      late = isLate(splitted)
      wrongFileName = True
      break

  #really odd filename
  if fileToGrade == "" :
    print(subprocess.getoutput('ls -l'))
    fileToGrade = input("Which file should I grade? ")
    if fileToGrade == "" :
      if writeToFile: 
        outputFile.write("0\tno file\n")
      os.chdir("..")
      return
    else :
      splitted = subprocess.getoutput('ls -l ' + fileToGrade.replace(' ','\ ')).split()
      late = isLate(splitted)
      wrongFileName = True

  #grading time!
  '''
  5 total test cases
  2 normal test each 5 points
    all 1's
    alternating 1 -1
  3 others
  test that it handles n correctly, 5 points
  test if inputting weird guesses is handled correctly, 5 points
  test that it handles 7 no's correctly, 5 points

  general formatting 5 points
  total of 30 points
  '''
  initialPrompt = "Guessing Game\n\nThink of a number between 1 and 100 inclusive.\nAnd I will guess what it is in 7 tries or less.\n\nAre you ready? (y/n): "
  prompt = "Enter 1 if my guess was high, -1 if low, and 0 if correct: "
  goodResponse = "Thank you for playing the Guessing Game."
  badResponse = "Either you guessed a number out of range or you had an incorrect entry."

  testCases = [
    'y\n1\n1\n1\n1\n1\n0',
    'y\n1\n-1\n1\n-1\n1\n-1\n0',
    'n\n',
    'y\nBAD INPUT\nBAD INPUT\n0\n',
    'y\n1\n-1\n1\n-1\n1\n-1\n1'
  ]
  correctOutput = [
    initialPrompt + '\nGuess  1 :  The number you thought was 50\nEnter 1 if my guess was high, -1 if low, and 0 if correct: \nGuess  2 :  The number you thought was 25\nEnter 1 if my guess was high, -1 if low, and 0 if correct: \nGuess  3 :  The number you thought was 12\nEnter 1 if my guess was high, -1 if low, and 0 if correct: \nGuess  4 :  The number you thought was 6\nEnter 1 if my guess was high, -1 if low, and 0 if correct: \nGuess  5 :  The number you thought was 3\nEnter 1 if my guess was high, -1 if low, and 0 if correct: \nGuess  6 :  The number you thought was 1\nEnter 1 if my guess was high, -1 if low, and 0 if correct: \n'+ goodResponse,
    initialPrompt + '\nGuess  1 :  The number you thought was 50\nEnter 1 if my guess was high, -1 if low, and 0 if correct: \nGuess  2 :  The number you thought was 25\nEnter 1 if my guess was high, -1 if low, and 0 if correct: \nGuess  3 :  The number you thought was 37\nEnter 1 if my guess was high, -1 if low, and 0 if correct: \nGuess  4 :  The number you thought was 31\nEnter 1 if my guess was high, -1 if low, and 0 if correct: \nGuess  5 :  The number you thought was 34\nEnter 1 if my guess was high, -1 if low, and 0 if correct: \nGuess  6 :  The number you thought was 32\nEnter 1 if my guess was high, -1 if low, and 0 if correct: \nGuess  7 :  The number you thought was 33\nEnter 1 if my guess was high, -1 if low, and 0 if correct: \n'+goodResponse,
    initialPrompt + '\nBye',
    initialPrompt + '\nGuess  1 :  The number you thought was 50\nEnter 1 if my guess was high, -1 if low, and 0 if correct: \nEnter 1 if my guess was high, -1 if low, and 0 if correct: \nEnter 1 if my guess was high, -1 if low, and 0 if correct: \n' + goodResponse,
    initialPrompt + '\nGuess  1 :  The number you thought was 50\nEnter 1 if my guess was high, -1 if low, and 0 if correct: \nGuess  2 :  The number you thought was 25\nEnter 1 if my guess was high, -1 if low, and 0 if correct: \nGuess  3 :  The number you thought was 37\nEnter 1 if my guess was high, -1 if low, and 0 if correct: \nGuess  4 :  The number you thought was 31\nEnter 1 if my guess was high, -1 if low, and 0 if correct: \nGuess  5 :  The number you thought was 34\nEnter 1 if my guess was high, -1 if low, and 0 if correct: \nGuess  6 :  The number you thought was 32\nEnter 1 if my guess was high, -1 if low, and 0 if correct: \nGuess  7 :  The number you thought was 33\nEnter 1 if my guess was high, -1 if low, and 0 if correct: \n'+badResponse
  ]
  if late != -1:
    answers = []
    count = 0
    for test in testCases:
      count += 1
      try:
        process = subprocess.Popen(['python3', fileToGrade], **pipes)
        out = process.communicate(bytes(test, 'UTF-8'))[0]
        answers.append(str(out)[2:-1].replace('\\n','\n').strip())
      except KeyboardInterrupt:
        print(" on test" +str(count))

    correct_formatting = True

    # gradin' normal tests 
    correct = ""
    count = 0
    numFailed = 0
    for correct,out in zip(correctOutput,answers):
      printed = False
      count += 1
      print("=====Test "+str(count)+"=====")
      #check formatting
      if out != correct and correct_formatting:
        print("\tIncorrect Formatting -5")
        print("\t=====Correct=====\n\t"+'\n\t'.join(correct.split('\n'))+"\n\t=====Output=====\n\t"+'\n\t'.join(out.split('\n')))
        printed = True
        correct_formatting = False

      #check correctness
      #if there's a perfect match no need to fuzzy match
      if not correct_formatting:
        failed = False
        theirNums = [int(s) for s in out.split() if s.isdigit()]
        ourNums = [int(s) for s in correct.split() if s.isdigit()]

        if count == 3:
          if "bye" not in out.lower(): 
            failed = True
        elif count == 5:
          if badResponse.lower()[:-1] not in out.lower(): 
            failed = True
        elif set(theirNums) != set(ourNums):
          failed = True

        if failed:
          comments.append("Failed test " + str(count) + " (-5)")
          numFailed += 1
          if not printed:
            print("\tFailed test "+str(count) + ": -5")
            print("\t=====Correct=====\n\t"+'\n\t'.join(correct.split('\n'))+"\n\t=====Output=====\n\t"+'\n\t'.join(out.split('\n')))
        else:
          print("\tPassed")
      else:
        print("\tPassed")

    #take off for test that we had to ^C
    if len(answers) < 5:
      numFailed += 5 - len(answers)

    #calculating grade time
    total_off = (numFailed * -5)
    print()
    print("Failed " + str(numFailed))

    if not correct_formatting:
      total_off -= 5
      feedback = "Incorrect formatting: -5"
      comments.append(feedback)
      print(feedback)
    grade += total_off
    print("Grade: "+str(grade)+"/70")

  #checking for header and style
  #os.system('vim ' + fileToGrade)
  input("Hit Enter to cat")
  print(subprocess.getoutput('cat ' + fileToGrade))
  headerInput = input("Header and comments? (y/n, hit enter for y): ")
  if headerInput == 'y' or headerInput == '' :
    header = True
  else :
    header = False
  style = input("Style/Other (Out of 30, hit enter for 30): ")
  gen_comments = input("General Comments?: ").rstrip().lstrip()
  gen_comments = gen_comments if len(gen_comments) is not 0 else "style"
  if not style.isdigit():
    style = 30
  else :
    style = int(style)
  if (gen_comments != "style" or style != 30):
    gen_comments += " (%+d)" % (style - 30)
    comments.append("%s" % gen_comments)
  
  #writing grade time!
  if late == -1:
    if writeToFile: outputFile.write('0\t More than 7 days late')
    print('Late more than 7 days!')
  else :
    if late == 3:
      comments.append("3 - 7 days late (-30)")
      grade -= 30
    elif late == 2 :
      comments.append("2 days late (-20)")
      grade -= 20
    elif late == 1 :
      comments.append("1 day late (-10)")
      grade -= 10
    
    if wrongFileName :
      comments.append("wrong filename (-10)")
      grade -= 10
    if not header :
      comments.append("missing comments or malformed header (-10)")
      grade -= 10

    if writeToFile: outputFile.write(str(grade+style) + "\t" + ', '.join(comments))
      
  if writeToFile: outputFile.write('\n')
  os.chdir("..")
Exemplo n.º 38
0
    def prepaire_repo(self, name):
        name = name.replace('-', '').replace(' ', '')
        ##### Model Files
        self.log.info("make repo ready vor upload")
        presiceversion = linecache.getline(
            self.file_system.path +
            "/precise/mycroft_precise.egg-info/PKG-INFO",
            3).replace('Version: ', '')[:5]
        modelzip = self.precisefolder + "/" + name + "/models/" + name + "-" + self.lang[:3] + presiceversion + "-" + time.strftime(
            "%Y%m%d") + "-" + self.settings.get('localgit') + ".tar.gz"
        if not os.path.isdir(self.precisefolder + "/" + name + "/models/"):
            os.makedirs(self.precisefolder + "/" + name + "/models/")
        tar = tarfile.open(modelzip, "w:gz")
        for nams in [
                self.file_system.path + "/" + name + ".pb",
                self.file_system.path + "/" + name + ".pbtxt",
                self.file_system.path + "/" + name + ".pb.params"
        ]:
            tar.add(nams)
        #### calculating info
        traininfo = linecache.getline(
            self.file_system.path + "/" + name + ".logs/output.txt", 2)
        #### generate Readme.md
        readmefile = self.precisefolder + "/" + name + "/models/README.md"
        file = open(readmefile, "a")
        if not os.path.isfile(readmefile):
            file.write("# " + name + "\n")
        file.write("\n### " + name + "-" + self.lang[:3] +
                   time.strftime("%Y%m%d") + "\n")
        file.write(presiceversion + " " + traininfo[:1] +
                   ". Use Public Domain Sounds Backup:" +
                   str(self.settings["soundbackup"]) +
                   ", automatically generated by wakeword trainer skill \n")
        file.close()

        ###### licenses
        licensefile = self.precisefolder + "/licenses/license-" + time.strftime(
            "%Y%m%d") + "-" + self.settings.get('localgit') + ".txt"
        fobj_in = open(self.precisefolder + "/licenses/license-template.txt",
                       "r")
        fobj_out = open(licensefile, "w")
        for line in fobj_in:
            line = line.replace(
                "I, [author name]", "I, " + self.settings.get('localgit') +
                ' (https://github.com/' + self.settings.get('localgit') + ')')
            line = line.replace(
                "/file/name/1",
                "automatically generated by gras64 wakeword trainer skill"
            ).replace("/file/name/2", "")
            fobj_out.write(str(line))
        modelzipfile = modelzip.replace(self.precisefolder + "/", "")
        fobj_out.write(modelzipfile + "\n")
        for root, dirs, files in os.walk(self.precisefolder + "/" + name +
                                         "/" + self.lang[:2]):
            for f in files:
                filename = os.path.join(root, f)
                self.log.info("filename: " + filename)
                if filename.endswith('.wav'):
                    filename = filename.replace(self.precisefolder, "")
                    fobj_out.write(filename + "\n")
        fobj_in.close()
        fobj_out.close()

        ##### Copy all wav file
        if not self.settings["onlyPrecise"]:
            source = self.settings[
                "file_path"] + name + "/wake-word/" + self.lang[:2] + "-short/"
            destination = self.precisefolder + "/" + name + "/" + self.lang + "/"
            if not os.path.isdir(destination):
                os.makedirs(destination)
            fobj_out = open(licensefile, "a")
            for filename in os.listdir(source):
                if filename.endswith('.wav'):
                    shutil.copy(source + filename, destination)
                    fobj_out.write("/" + name + "/" + self.lang[:2] + "/" +
                                   filename + "\n")
            source = self.settings[
                "file_path"] + name + "/test/wake-word/" + self.lang[:2] + "-short/"
            for filename in os.listdir(source):
                if filename.endswith('.wav'):
                    shutil.copy(source + filename, destination)
                    fobj_out.write("/" + name + "/" + self.lang[:2] + "/" +
                                   filename + "\n")
            fobj_out.close()
def assign10(csid , writeToFile) :
  fileToGrade = ""
  late = 0
  grade = 70
  style = 30
  wrongFileName = False
  header = True
  comments = []

  os.chdir(csid)
  if writeToFile: outputFile.write(csid + "\t")
  files = os.listdir('.')

  #filename checking
  for f in files :
    splitted = subprocess.getoutput('ls -l ' + f).split()
    if f == filename :
      fileToGrade = filename
      late = isLate(splitted)
      break
    elif f == filename.lower() :
      fileToGrade = filename.lower()
      late = isLate(splitted)
      wrongFileName = True
      break

  #really odd filename
  if fileToGrade == "" :
    print(subprocess.getoutput('ls -l'))
    fileToGrade = input("Which file should I grade? ")
    if fileToGrade == "" :
      if writeToFile: 
        outputFile.write("0\tno file\n")
      os.chdir("..")
      return
    else :
      splitted = subprocess.getoutput('ls -l ' + fileToGrade.replace(' ','\ ')).split()
      late = isLate(splitted)
      wrongFileName = True


  # copies ../FILE_TO_COPY to ./ and runs the program in ./
  # diffs output.txt with FILE_CORRECT
  # returns tuple (file output matches, stdout output matches)
  def cp_run_and_diff (stdin_text, file_to_copy, file_correct, stdout_correct):
    os.chdir('..')
    subprocess.getoutput('rm %soutput.txt' % csid)
    os.system('cp %s %s/input.txt' % (file_to_copy, csid))
    os.chdir(csid)
    process = subprocess.Popen(['python3', fileToGrade], **pipes)
    stdout_output = str(process.communicate(bytes(stdin_text, 'UTF-8'))[0])[2:-1]
    print("%s %s" % ("Encrypting" if stdin_text == 'E' else "Decrypting", file_to_copy))
    try:
      differences = subprocess.getoutput('diff -w output.txt ../%s' % file_correct)
      if differences != '':
        print("Expected:")
        line = ""
        [print("\t|%s" % line.strip()) for line in open('../%s' % file_correct, 'r')]
        print("Actual:")
        line = ""
        [print("\t|%s" % line.strip()) for line in open('output.txt', 'r')]
      else:
        print("File outputs match")
    except:
      print("Program did not output to file.")
    subprocess.getoutput('rm output.txt')
    subprocess.getoutput('rm input.txt')
    stdout_output = '\n'.join([line if line.count(' ') != len(line) else '\n' for line in stdout_output.replace(r'\n', '\n').replace('decrpyt', 'decrypt').strip().split('\n')])
    print("STDOUT matches\n" if stdout_output == stdout_correct else "STDOUT does not match:\n%s\n" % stdout_output)
    return (differences == '', stdout_output == stdout_correct)

  if late != -1:
    # encrypting (3 tests; worth 4 points each; max 15)
    encrypt_tests = []
    # decrypting (3 tests: worth 4 points each; max 15)
    decrypt_tests = []
    # program formatting (1 test: worth 5 points)
    format_test = True
    correct_format = 'Do you want to encrypt or decrypt? (E / D): \nOutput written to output.txt'

    for decrypted, encrypted in zip (files_to_encrypt, files_to_decrypt):
      (correctness, format_test1) = cp_run_and_diff ('E', decrypted, encrypted, correct_format)
      encrypt_tests.append(correctness)
      (correctness, format_test2) = cp_run_and_diff ('D', encrypted, decrypted, correct_format)
      decrypt_tests.append(correctness)
      format_test = format_test and format_test1 and format_test2

    
      
    if all(encrypt_tests) and all(decrypt_tests) and format_test:
      print("Perfect! ^_^")
      comments.append("passed all tests")
    elif not (any(encrypt_tests) or any(decrypt_tests) or format_test):
      print("Failed every test... ='(")
      comments.append("failed all tests (-30)")
      grade -= 30
    else:
      first_fail = True

      def print_fail (string):
        nonlocal first_fail
        if first_fail:
          first_fail = False
          print(string)

      functionality_fail = "Tests failed (5 points each):"
      num_off = 0
      if not encrypt_tests[0]:
        print_fail (functionality_fail)
        print("\tEncrypt even length")
        comments.append("failed encrypt even (-5)")
        num_off += 5
      if not encrypt_tests[1]:
        print_fail (functionality_fail)
        print("\tEncrypt odd length")
        comments.append("failed encrypt odd (-5)")
        num_off += 5
      if not encrypt_tests[2]:
        print_fail (functionality_fail)
        print("\tEncrypt multiple lines")
        comments.append("failed encrypt multi (-5)")
        num_off += 5
      if not decrypt_tests[0]:
        print_fail (functionality_fail)
        print("\tDecrypt even length")
        comments.append("failed decrypt even (-5)")
        num_off += 5
      if not decrypt_tests[1]:
        print_fail (functionality_fail)
        print("\tDecrypt odd length")
        comments.append("failed decrypt odd (-5)")
        num_off += 5
      if not decrypt_tests[2]:
        print_fail (functionality_fail)
        print("\tDecrypt multiple lines")
        comments.append("failed decrypt multi (-5)")
        num_off += 5
      if not format_test:
        print_fail (functionality_fail)
        print("\tSTDOUT Formatting")
        comments.append("incorrect formatting (-5)")
        num_off += 5
      grade -= num_off
      print("Total off: (-%d)" % num_off)

  #checking for header and style
  #os.system('vim ' + fileToGrade)
  input("Hit Enter to cat")
  print(subprocess.getoutput('cat ' + fileToGrade))
  headerInput = input("Header and comments? (y/n, hit enter for y): ")
  if headerInput == 'y' or headerInput == '' :
    header = True
  else :
    header = False
  style = input("Style/Other (Out of 30, hit enter for 30): ")
  gen_comments = input("General Comments?: ").rstrip().lstrip()
  gen_comments = gen_comments if len(gen_comments) is not 0 else "style"
  if not style.isdigit():
    style = 30
  else :
    style = int(style)
  if (gen_comments != "style" or style != 30):
    gen_comments += " (%+d)" % (style - 30)
    comments.append("%s" % gen_comments)
  
  #writing grade time!
  if late == -1:
    if writeToFile: outputFile.write('0\t More than 7 days late')
    print('Late more than 7 days!')
  else :
    if late == 3:
      comments.append("3 - 7 days late (-30)")
      grade -= 30
    elif late == 2 :
      comments.append("2 days late (-20)")
      grade -= 20
    elif late == 1 :
      comments.append("1 day late (-10)")
      grade -= 10
    
    if wrongFileName :
      comments.append("wrong filename (-10)")
      grade -= 10
    if not header :
      comments.append("missing comments or malformed header (-10)")
      grade -= 10

    if writeToFile: outputFile.write(str(grade+style) + "\t" + ', '.join(comments))
      
  if writeToFile: outputFile.write('\n')
  os.chdir("..")
Exemplo n.º 40
0
def assign6( csid , writeToFile) :
  fileToGrade = ""
  late = 0
  grade = 70
  style = 30
  wrongFileName = False
  header = True
  comments = " "

  os.chdir(csid)
  if writeToFile: outputFile.write(csid + "\t")
  files = os.listdir('.')

  #filename checking
  for f in files :
    splitted = subprocess.getoutput('ls -l ' + f).split()
    if f == filename :
      fileToGrade = filename
      late = isLate(splitted)
      break
    elif f == filename.lower() :
      fileToGrade = filename.lower()
      late = isLate(splitted)
      wrongFileName = True
      break

  #really odd filename
  if fileToGrade == "" :
    print(subprocess.getoutput('ls -l'))
    fileToGrade = input("Which file should I grade? ")
    if fileToGrade == "" :
      if writeToFile: 
        outputFile.write("0\tno file\n")
      os.chdir("..")
      return
    else :
      splitted = subprocess.getoutput('ls -l ' + fileToGrade.replace(' ','\ ')).split()
      late = isLate(splitted)
      wrongFileName = True

  #grading time!
  if not fileToGrade == "" and late != -1:
    answers = []
    for x in range(len(inputArray)):
      process = subprocess.Popen(['python3', fileToGrade], stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
      try:
        out = process.communicate(bytes('\n'.join(inputArray[x].split()) + '\n', 'UTF-8'))[0]
      except KeyboardInterrupt:
        pass
      answers.append(str(out)[2:-1])

    count = 0
    perfectCount = 0
    closeCount = 0 
    wrongCount = 0
    for answer, cor in zip(answers,correct):
      cor = cor.split()
      #extracting relevant data form the students output
      nums = re.findall("\D+(\d+)\D+(\d+)", answer)
      if len(nums) != 1:
        wrongCount += 1
        continue
      elif len(nums[0]) != 2:
        wrongCount += 1
        continue
      longNum = int(nums[0][0])
      cycleLength = int(nums[0][1])

      #perfect check
      if count >= 6: #negative testing time! 
        perfect = "Enter starting number of the range: \nEnter ending number of the range: \n" * 6 +"The number " + str(cor[0]) + " has the longest cycle length of " + str(cor[1]+".")
      else:
        perfect = "Enter starting number of the range: \nEnter ending number of the range: \nThe number " + str(cor[0]) + " has the longest cycle length of " + str(cor[1]+".")

      if perfect == answer.replace('\\n','\n').rstrip('\n'):
        print('Perfect answer for #', count + 1)
        perfectCount +=1
      elif longNum == int(cor[0]) and cycleLength == int(cor[1]):
        print('Close answer for #', count + 1)
        print(perfect+"\n\tvs.\n\t"+answer.replace('\\n','\n\t'))
        closeCount += 1
      else:
        print('Wrong answer for #', count + 1)
        print(perfect+"\n\tvs.\n\t"+answer.replace('\\n','\n\t'))
        wrongCount += 1
      count += 1

    print("Perfect:", str(perfectCount) + "/10")
    print("Close:", str(closeCount) + "/10")
    print("Wrong:", str(wrongCount) + "/10")
    if wrongCount != 0 or closeCount != 0:
        grade -= (3 * wrongCount) 
        grade -= (1 * closeCount)
        comments += " Output did not match instructors P: "+str(perfectCount)+" C: "+str(closeCount)+" W: "+str(wrongCount)+ ", "


  #checking for header and style
  #os.system('vim ' + fileToGrade)
  input("Hit Enter to cat")
  print(subprocess.getoutput('cat ' + fileToGrade))
  headerInput = input("Header( (y or enter)  /  n)? ")
  if headerInput == 'y' or headerInput == '' :
    header = True
  else :
    header = False
  style = input("Style/Comments (Enter a number out of 30 to represent their grade, hit enter for 30): ")
  comments += input ("General Comments?:  ")
  if not style.isdigit() :
    style = 30
  else :
    style = int(style)
  
  #writing grade time!
  if late == -1:
    if writeToFile: outputFile.write('0\t More than 7 days late')
  else :
    if late == 3:
      comments = "3 - 7 days late, "
      grade -= 30
    elif late == 2 :
      comments = "2 days late, "
      grade -= 20
    elif late == 1 :
      comments = "1 day late, "
      grade -= 10
    
    if wrongFileName :
      comments += " wrong filename, "
      grade -= 10
    if not header :
      comments += " no/malformed header, "
      grade -= 10

    if writeToFile: outputFile.write(str(grade+style) + "\t"+comments.rstrip(', '))
      
  if writeToFile: outputFile.write('\n')
  os.chdir("..")
Exemplo n.º 41
0
def main():

    parser = argparse.ArgumentParser(
        description='Generate playlists with the indicated length')
    parser.add_argument('-d',
                        '--directory',
                        help='Directory with music files',
                        type=str,
                        required=True)
    parser.add_argument('-l',
                        '--length',
                        help='Length of the playlist, in minutes',
                        type=int,
                        required=True)

    args = parser.parse_args()

    directory = args.directory
    length = args.length * 60

    path = r'./playlists/'
    if not os.path.exists(path): os.makedirs(path)

    playlist_basename = basename(argv[0][:-3]) + str(length / 60) + '_'
    playlist_number = 1
    curr_length = 0
    curr_items = []
    too_long_items = []
    all_items = []

    for music_file in os.listdir(directory):
        if fnmatch.fnmatch(music_file, '*.mp[43]'):
            all_items.append(directory + music_file)

    shuffle(all_items)

    for item in all_items:
        if curr_length >= length:
            name = path + playlist_basename + str(playlist_number) + '.m3u'
            playlist_file = open(name, 'w')
            playlist_file.writelines(curr_items)
            playlist_file.close()
            print 'Playlist generated, name: ', name, ' length ', curr_length / 60, 'min'
            playlist_number += 1
            curr_length = 0
            curr_items = []
        else:
            encoding = item[-4:]
            encodings = {'.mp3': MP3, '.mp4': MP4}
            try:
                music_file = encodings[encoding](item)
            except Exception as e:
                handleException(e)
            else:
                file_length = music_file.info.length
                if file_length > length:
                    too_long_items.append(item)
                    print 'File %s exceed the given length (%s min)' % (
                        item, file_length / 60)
                else:
                    curr_length += file_length
                    curr_items.append(item + '\n')

    print '\nThis files exceeded the given length and were not added to any playlist...\n'
    for i in too_long_items:
        print basename(i)
 def xml_path(self):
     """ Return path to xml file in granule folder."""
     fname = [f for f in os.listdir(self._path) if ".xml" in f][0]
     return join(self._path, fname)
Exemplo n.º 43
0
def assign11(csid , writeToFile) :
  fileToGrade = ""
  late = 0
  grade = 70
  style = 30
  wrongFileName = False
  header = True
  comments = []

  os.chdir(csid)
  if writeToFile: outputFile.write(csid + "\t")
  files = os.listdir('.')

  #filename checking
  for f in files :
    splitted = subprocess.getoutput('ls -l ' + f).split()
    if f == filename :
      fileToGrade = filename
      late = isLate(splitted)
      break
    elif f == filename.lower() :
      fileToGrade = filename.lower()
      late = isLate(splitted)
      wrongFileName = True
      break

  #really odd filename
  if fileToGrade == "" :
    print(subprocess.getoutput('ls -l'))
    fileToGrade = input("Which file should I grade? ")
    if fileToGrade == "" :
      if writeToFile: 
        outputFile.write("0\tno file\n")
      os.chdir("..")
      return
    else :
      splitted = subprocess.getoutput('ls -l ' + fileToGrade.replace(' ','\ ')).split()
      late = isLate(splitted)
      wrongFileName = True

  #grading time!
  '''
  tests 1-6 are 3 points
  7 and 8 are for multiple subsequences and are 4 points
  test 9 is to make sure they don't output singleton matches and is 3 points
  '''
  if late != -1:
    answers = []
    count = 0
    for strand1,strand2 in zip(inputLines[0::2],inputLines[1::2]):
      count += 1
      try:
        process = subprocess.Popen(['python3', fileToGrade], **pipes)
        out = process.communicate(bytes(strand1+'\n'+strand2+'\n', 'UTF-8'))[0]
        answers.append(str(out)[2:-1].replace('\\n','\n').strip())
      except KeyboardInterrupt:
        print(" on test" +str(count))

    correct_formatting = True
    extra_credit = True
    num_3_points_tests_failed = 0
    num_4_points_tests_failed = 0

    # gradin' normal tests 
    correct = ""
    count = 0
    for normal_out,extra_credit_out,out in zip(correctLines[0::2],correctLines[1::2],answers[:10]):
      printed = False
      correct_formatting = True
      count += 1
      normal_out = normal_out.replace('\\n','\n')
      print("\n=====Test "+str(count)+"=====")
      extra_credit_out = extra_credit_out.replace('\\n','\n')
      # there was some ambiguity on if the no sequence found text had a period or not
      out = out.replace('.','')
      if extra_credit and out != extra_credit_out:
        extra_credit = False
      if not extra_credit and correct_formatting and out != normal_out :
        print("\tIncorrect Formatting -5")
        print("\t=====Correct=====\n"+normal_out+"\n\t=====Output=====\n"+out)
        if input("Hit enter to accept deduction, or type n: ") == "":
          printed = True
          correct_formatting = False

      #check correctness
      #if there's a perfect match no need to fuzzy match
      if not correct_formatting:
        failed = False
        splitted = set(out.split(':')[-1].split('\n')[1:])
        correct_splitted = normal_out.split('\n')
        if count <= 6:
          if splitted != set(correct_splitted[-1:]):
            num_3_points_tests_failed += 1
            failed = True
            points = "3"
        elif count == 7:
          if splitted != set(correct_splitted[-2:]):
            num_4_points_tests_failed += 1
            failed = True
            points = "4"
        elif count == 8:
          if splitted != set(correct_splitted[-3:]):
            num_4_points_tests_failed += 1
            failed = True
            points = "4"
        elif count == 9:
          if splitted != set(correct_splitted[-1:]):
            num_3_points_tests_failed += 1
            failed = True
            points = "3"
        if failed:
          if not printed:
            print("\t=====Correct=====\n"+normal_out+"\n\t=====Output=====\n"+out)
          print("\tFailed Test -"+points)
        else:
          print("\tPassed")
      else:
        print("\tPassed")

    #calculating grade time
    total_off = 0
    total_off -= num_3_points_tests_failed * 3
    total_off -= num_4_points_tests_failed * 4
    print()
    feedback = "3 point tests passed: "+str(7-num_3_points_tests_failed)+"/7 so " + str(num_3_points_tests_failed*-3)
    comments.append(feedback)
    print(feedback)
    feedback = "4 point tests passed: "+str(2-num_4_points_tests_failed)+"/2 so " + str(num_4_points_tests_failed*-4)
    comments.append(feedback)
    print(feedback)

    if not correct_formatting:
      total_off -= 5
      feedback = "Incorrect formatting: -5"
      comments.append(feedback)
      print(feedback)
    if extra_credit:
      total_off +=3
      feedback = "Extra credit formatting: +3"
      comments.append(feedback)
      print(feedback)
    grade += total_off
    print("Grade: "+str(grade)+"/70")
  #checking for header and style
  #os.system('vim ' + fileToGrade)
  input("Hit Enter to cat")
  print(subprocess.getoutput('cat ' + fileToGrade))
  headerInput = input("Header and comments? (y/n, hit enter for y): ")
  if headerInput == 'y' or headerInput == '' :
    header = True
  else :
    header = False
  style = input("Style/Other (Out of 30, hit enter for 30): ")
  gen_comments = input("General Comments?: ").rstrip().lstrip()
  gen_comments = gen_comments if len(gen_comments) is not 0 else "style"
  if not style.isdigit():
    style = 30
  else :
    style = int(style)
  if (gen_comments != "style" or style != 30):
    gen_comments += " (%+d)" % (style - 30)
    comments.append("%s" % gen_comments)
  
  #writing grade time!
  if late == -1:
    if writeToFile: outputFile.write('0\t More than 7 days late')
    print('Late more than 7 days!')
  else :
    if late == 3:
      comments.append("3 - 7 days late (-30)")
      grade -= 30
    elif late == 2 :
      comments.append("2 days late (-20)")
      grade -= 20
    elif late == 1 :
      comments.append("1 day late (-10)")
      grade -= 10
    
    if wrongFileName :
      comments.append("wrong filename (-10)")
      grade -= 10
    if not header :
      comments.append("missing comments or malformed header (-10)")
      grade -= 10

    if writeToFile: outputFile.write(str(grade+style) + "\t" + ', '.join(comments))
      
  if writeToFile: outputFile.write('\n')
  os.chdir("..")
def assign15(csid , writeToFile) :
  fileToGrade = ""
  late = 0
  grade = 70
  style = 30
  wrongFileName = False
  header = True
  comments = []

  os.chdir(csid)
  if writeToFile: outputFile.write(csid + "\t")
  files = os.listdir('.')

  #filename checking
  for f in files :
    splitted = subprocess.getoutput('ls -l ' + f).split()
    if f == filename :
      fileToGrade = filename
      late = isLate(splitted)
      break
    elif f == filename.lower() :
      fileToGrade = filename.lower()
      late = isLate(splitted)
      wrongFileName = True
      break

  #really odd filename
  if fileToGrade == "" :
    print(subprocess.getoutput('ls -l'))
    fileToGrade = input("Which file should I grade? ")
    if fileToGrade == "" :
      if writeToFile: 
        outputFile.write("0\tno file\n")
      os.chdir("..")
      return
    else :
      splitted = subprocess.getoutput('ls -l ' + fileToGrade.replace(' ','\ ')).split()
      late = isLate(splitted)
      wrongFileName = True

  #grading time!
  '''
  20 points for correctness
  -5 if there's not 1 number past the decimal point
  '''
  if late != -1:
    #copy over input file
    os.system('cp ../Census_2009.txt .')
    try:
      process = subprocess.Popen(['python3', fileToGrade], **pipes)
      out = process.communicate(bytes('', 'UTF-8'))[0]
      answerList = str(out)[2:-1].replace('\\t',' ').strip().split('\\n')
    except KeyboardInterrupt:
      print(" on test" +str(count))

    correct_formatting = True
    correct_output = True

    f = open('../correct.txt', 'r')
    correctList = f.read()
    f.close()

    correct = []
    for x in correctList.split('\n'):
      correct.append(x.split(' '))
    answer = []
    for x in answerList:
      if x.strip(): #empty string is false
        answer.append(x.split())

    #check correctness
    if len(correct) == len(answer):
      count = 0
      for right,theirs in zip(correct,answer):
        count+=1
        if count == 1:
          continue
        for rightElem, theirsElem in zip(right,theirs):
          if rightElem != theirsElem and (rightElem not in theirsElem and theirsElem not in rightElem):
            try:
              rightFloat = eval(rightElem)
              theirsFloat = eval(theirsElem)
            except Exception:
              rightFloat = ""
              theirsFloat = ""
            #SOOOOO MUCH COPY PASTA!!!! DON'T EVEN CAR IT WORKS!!!
            #YES CAR!!!!
            if type(rightFloat) == type(0.0) and type(theirsFloat) == type(0.0):
              if rightFloat == round(theirsFloat,1):
                correct_formatting = False
                print('Mismatch!')
                print('===Correct===')
                print(rightElem)
                print('===Theirs===')
                print(theirsElem)
              else:                
                print('Mismatch!')
                print('===Correct===')
                print(rightElem)
                print('===Theirs===')
                print(theirsElem)
                correct_output = False
            else:                
              print('Mismatch!')
              print('===Correct===')
              print(rightElem)
              print('===Theirs===')
              print(theirsElem)
              correct_output = False
    else:
      correct_output = False
      print("Lengths didn't match up?!?")
      print('===Correct===')
      for x in correct:
        print(x)
      print('===Theirs===')
      for x in answer:
        print(x)

    if not correct_output:
      print('===Correct===')
      for x in correct:
        print(x)
      print('===Theirs===')
      for x in answer:
        print(x)


    #calculating grade time
    total_off = 0
    if not correct_output:
      total_off -=20
      #Yup, kc.
      feedbakc = "Failed: -20 =("
      comments.append(feedbakc)
      print(feedbakc)
    if not correct_formatting:
      total_off -= 5
      feedback = "Incorrect formatting: -5"
      comments.append(feedback)
      print(feedback)
    grade += total_off
    if total_off == 0:
      print("<('.')^ Perfect ^('.')>")
    print("Grade: "+str(grade)+"/70")

  #checking for header and style
  #os.system('vim ' + fileToGrade)
  input("Hit Enter to cat")
  print(subprocess.getoutput('cat ' + fileToGrade))
  headerInput = input("Header and comments? (y/n, hit enter for y): ")
  if headerInput == 'y' or headerInput == '' :
    header = True
  else :
    header = False
  style = input("Style/Other (Out of 30, hit enter for 30): ")
  gen_comments = input("General Comments?: ").rstrip().lstrip()
  gen_comments = gen_comments if len(gen_comments) is not 0 else "style"
  if not style.isdigit():
    style = 30
  else :
    style = int(style)
  if (gen_comments != "style" or style != 30):
    gen_comments += " (%+d)" % (style - 30)
    comments.append("%s" % gen_comments)
  
  #writing grade time!
  if late == -1:
    if writeToFile: outputFile.write('0\t More than 7 days late')
    print('Late more than 7 days!')
  else :
    if late == 3:
      comments.append("3 - 7 days late (-30)")
      grade -= 30
    elif late == 2 :
      comments.append("2 days late (-20)")
      grade -= 20
    elif late == 1 :
      comments.append("1 day late (-10)")
      grade -= 10
    
    if wrongFileName :
      comments.append("wrong filename (-10)")
      grade -= 10
    if not header :
      comments.append("missing comments or malformed header (-10)")
      grade -= 10

    if writeToFile: outputFile.write(str(grade+style) + "\t" + ', '.join(comments))
      
  if writeToFile: outputFile.write('\n')
  os.chdir("..")
Exemplo n.º 45
0
from os.path import os
from urllib.request import Request
import pymysql
from bs4 import BeautifulSoup
import json


def get_dir_list(base_dir, son_dir):
    path_use = os.path.join(base_dir, son_dir)
    ret = os.listdir(path=path_use)
    return ret


if __name__ == '__main__':
    fileDir = 'D:/hanfuba/hanfutianxia'
    ziMuLu = os.listdir(path=fileDir)
    for i in ziMuLu:
        sunDir = fileDir + '/' + i
        sunMuLu = os.listdir(path=sunDir)
        conn = pymysql.connect(host='localhost',
                               user='******',
                               passwd='admin',
                               db='hanfuxin',
                               port=3306,
                               charset='utf8')
        cur = conn.cursor()
        for j in sunMuLu:
            htmlDir = sunDir + '/' + j
            htmlFile = open(htmlDir, encoding='utf-8')
            suo_you_nei_rong = str(htmlFile.readlines())
            soup_all = BeautifulSoup(suo_you_nei_rong, 'html.parser')
Exemplo n.º 46
0
def assign2( csid , writeToFile) :
  fileToGrade = ""
  late = 0
  linesWrong = 0
  grade = 70
  style = 30
  wrongFileName = False
  header = True
  comments = " "
  manualComments = ""

  os.chdir(csid)
  if writeToFile: outputFile.write(csid + "\t")
  files = os.listdir('.')

  #filename checking
  for f in files :
    splitted = subprocess.getoutput('ls -l ' + f).split()
    if f == filename :
      fileToGrade = filename
      late = isLate(splitted)
      break
    elif f == filename.lower() :
      fileToGrade = filename.lower()
      late = isLate(splitted)
      wrongFileName = True
      break

  #really odd filename
  if fileToGrade == "" :
    print(subprocess.getoutput('ls -l'))
    fileToGrade = input("Which file should I grade? ")
    if fileToGrade == "" :
      if writeToFile: 
        outputFile.write("0\tno file")
        os.chdir("..")
        return
    else :
      splitted = subprocess.getoutput('ls -l ' + fileToGrade.replace(' ','\ ')).split()
      late = isLate(splitted)
      wrongFileName = True

  #grading time!
  if not fileToGrade == "" and late < 3:
    answers = []
    for creditCardNumber in inputArray:
      process = subprocess.Popen(['python3', fileToGrade], stdin = subprocess.PIPE, stdout = subprocess.PIPE)
      out = process.communicate(bytes(creditCardNumber, 'UTF-8'))[0]
      answers.append(str(out)[2:-1])
     
    perfectCount = 0
    closeCount = 0 
    answerCount = 0
    wrongCount = 0
    for correctAnswer in correct.splitlines():
      if correctAnswer in answers[answerCount]:  #Does not contain the correct formatted answer
        print('Correct answer for #', answerCount+1)
        perfectCount += 1
      else:
        if correctAnswer.lower()[0:5] in answers[answerCount].lower() and not "in" + correctAnswer.lower()[0:5] in answers[answerCount].lower(): #We can change to to account for invalid or not
          print ("Correct answer for #", answerCount+1," but incorrect formatting")
          print ("\t", correctAnswer[0:1],"-",answers[answerCount])
          closeCount += 1
        else:
          print("Wrong answer")
          wrongCount += 1
      answerCount += 1
    print("Perfect:", str(perfectCount) + "/8")
    print("Close:", str(closeCount) + "/8")
    print("Wrong:", str(wrongCount) + "/8")
    if(0 >= closeCount >= 4):
    	grade = 70 - (4 * wrongCount) 
    	comments += " Output did not match instructors, "
    elif(closeCount >= 4):
    	grade = 60 - (4 * wrongCount)
    	comments += " Output did not match instructors, "

      #TODO take of 5 points if they're closeCount is 0 to 4 and take off 10 if closeCount is greater than 4
      #TODO take off 4 * wrongCount points as well
      #TODO fix the else below to output the correct comments
     
  else:
    #print('Their output:')
    #print(out)
    #print('Correct output:')
    #print(correct)
    #print('logo has ' + str(len(lines)) +' lines, not 19')
    #don't dock points for lateness or wrong filename here
    gradeInput = input("Grade out of 70 (no style, hit enter if 65): ")
    if gradeInput == '' :
      grade = 65
    else :
      grade = int(gradeInput)
    manualComments = input("Comments: ")

  #checking for header and style
  #os.system('vim ' + fileToGrade)
  input("Hit Enter to cat")
  print(subprocess.getoutput('cat ' + fileToGrade))
  headerInput = input("Header( (y or enter)  /  n)? ")
  if headerInput == 'y' or headerInput == '' :
    header = True
  else :
    header = False
  style = input("Style/Comments (Enter a number out of 30 to represent their grade, hit enter for 30): ")
  comments += input ("General Comments?:  ")
  if not style.isdigit() :
    style = 30
  else :
    style = int(style)
  
  #writing grade time!
  if late == 3 :
    if writeToFile: outputFile.write('0\t 3 days late')
  else :
    if late == 2 :
      comments = "2 days late, "
      grade -= 20
    elif late == 1 :
      comments = "1 day late, "
      grade -= 10
    
    if wrongFileName :
      comments += " wrong filename, "
      grade -= 10
    if not header :
      comments += " no/malformed header, "
      grade -= 10

    if linesWrong > 0 and linesWrong < 3 : 
      comments += "improperly formed superman logo, "
      grade -= 5
    elif linesWrong > 3 :
      comments += "nonsensical superman logo, "
      grade -= 5
    if writeToFile: outputFile.write(str(grade+style) + "\t"+comments.rstrip(', ') + manualComments)
      
  if writeToFile: outputFile.write('\n')
  os.chdir("..")
Exemplo n.º 47
0
def assign14(csid , writeToFile) :
  fileToGrade = ""
  late = 0
  grade = 70
  style = 30
  wrongFileName = False
  header = True
  comments = []

  os.chdir(csid)
  if writeToFile: outputFile.write(csid + "\t")
  files = os.listdir('.')

  #filename checking
  for f in files :
    splitted = subprocess.getoutput('ls -l ' + f).split()
    if f == filename :
      fileToGrade = filename
      late = isLate(splitted)
      break
    elif f == filename.lower() :
      fileToGrade = filename.lower()
      late = isLate(splitted)
      wrongFileName = True
      break

  #really odd filename
  if fileToGrade == "" :
    print(subprocess.getoutput('ls -l'))
    fileToGrade = input("Which file should I grade? ")
    if fileToGrade == "" :
      if writeToFile: 
        outputFile.write("0\tno file\n")
      os.chdir("..")
      return
    else :
      splitted = subprocess.getoutput('ls -l ' + fileToGrade.replace(' ','\ ')).split()
      late = isLate(splitted)
      wrongFileName = True

  #grading time!
  '''
  5 total test cases
  2 normal test each 5 points
    all 1's
    alternating 1 -1
  3 others
  test that it handles n correctly, 5 points
  test if inputting weird guesses is handled correctly, 5 points
  test that it handles 7 no's correctly, 5 points

  general formatting 5 points
  total of 30 points
  '''
  initialPrompt = "Guessing Game\n\nThink of a number between 1 and 100 inclusive.\nAnd I will guess what it is in 7 tries or less.\n\nAre you ready? (y/n): "
  prompt = "Enter 1 if my guess was high, -1 if low, and 0 if correct: "
  goodResponse = "Thank you for playing the Guessing Game."
  badResponse = "Either you guessed a number out of range or you had an incorrect entry."

  testCases = [
    'y\n1\n1\n1\n1\n1\n0',
    'y\n1\n-1\n1\n-1\n1\n-1\n0',
    'n\n',
    'y\nBAD INPUT\nBAD INPUT\n0\n',
    'y\n1\n-1\n1\n-1\n1\n-1\n1'
  ]
  testDescription = [
    'Thinking of 1',
    'Thinking of 33',
    "User doesn't want to play",
    'Entering something other than 1, -1, or 0',
    'More than 7 guesses should fail'
  ]
  correctOutput = [
    initialPrompt + '\nGuess  1 :  The number you thought was 50\nEnter 1 if my guess was high, -1 if low, and 0 if correct: \nGuess  2 :  The number you thought was 25\nEnter 1 if my guess was high, -1 if low, and 0 if correct: \nGuess  3 :  The number you thought was 12\nEnter 1 if my guess was high, -1 if low, and 0 if correct: \nGuess  4 :  The number you thought was 6\nEnter 1 if my guess was high, -1 if low, and 0 if correct: \nGuess  5 :  The number you thought was 3\nEnter 1 if my guess was high, -1 if low, and 0 if correct: \nGuess  6 :  The number you thought was 1\nEnter 1 if my guess was high, -1 if low, and 0 if correct: \n'+ goodResponse,
    initialPrompt + '\nGuess  1 :  The number you thought was 50\nEnter 1 if my guess was high, -1 if low, and 0 if correct: \nGuess  2 :  The number you thought was 25\nEnter 1 if my guess was high, -1 if low, and 0 if correct: \nGuess  3 :  The number you thought was 37\nEnter 1 if my guess was high, -1 if low, and 0 if correct: \nGuess  4 :  The number you thought was 31\nEnter 1 if my guess was high, -1 if low, and 0 if correct: \nGuess  5 :  The number you thought was 34\nEnter 1 if my guess was high, -1 if low, and 0 if correct: \nGuess  6 :  The number you thought was 32\nEnter 1 if my guess was high, -1 if low, and 0 if correct: \nGuess  7 :  The number you thought was 33\nEnter 1 if my guess was high, -1 if low, and 0 if correct: \n'+goodResponse,
    initialPrompt + '\nBye',
    initialPrompt + '\nGuess  1 :  The number you thought was 50\nEnter 1 if my guess was high, -1 if low, and 0 if correct: \nEnter 1 if my guess was high, -1 if low, and 0 if correct: \nEnter 1 if my guess was high, -1 if low, and 0 if correct: \n' + goodResponse,
    initialPrompt + '\nGuess  1 :  The number you thought was 50\nEnter 1 if my guess was high, -1 if low, and 0 if correct: \nGuess  2 :  The number you thought was 25\nEnter 1 if my guess was high, -1 if low, and 0 if correct: \nGuess  3 :  The number you thought was 37\nEnter 1 if my guess was high, -1 if low, and 0 if correct: \nGuess  4 :  The number you thought was 31\nEnter 1 if my guess was high, -1 if low, and 0 if correct: \nGuess  5 :  The number you thought was 34\nEnter 1 if my guess was high, -1 if low, and 0 if correct: \nGuess  6 :  The number you thought was 32\nEnter 1 if my guess was high, -1 if low, and 0 if correct: \nGuess  7 :  The number you thought was 33\nEnter 1 if my guess was high, -1 if low, and 0 if correct: \n'+badResponse
  ]

  if late != -1:
    answers = []
    for i, test in enumerate(testCases):
      try:
        process = subprocess.Popen(['python3', fileToGrade], **pipes)
        out = process.communicate(bytes(test, 'UTF-8'))[0]
        answers.append(str(out)[2:-1].replace('\\n','\n').strip())
      except KeyboardInterrupt:
        print(" on test" +str(i+1))

    # Check formatting using the first test case.
    correct_formatting = True
    answer_lines = answers[0].replace('  ', ' ').split('\n')
    correct_lines = correctOutput[0].replace('  ', ' ').split('\n')
    if len(answer_lines) != len(correct_lines):
      formatting_mistake = "Output has the wrong number of lines."
      correct_formatting = False
    elif "Guessing Game" not in answer_lines[0]:
      formatting_mistake = "First line in output is incorrect."
      correct_formatting = False
    elif "Think of a number between 1 and 100 inclusive." not in answer_lines[2] or \
         "And I will guess what it is in 7 tries or less." not in answer_lines[3]:
      formatting_mistake = "Game intro is missing or incorrect."
      correct_formatting = False
    elif "Are you ready? (y/n):" not in answer_lines[5]:
      formatting_mistake = "Did not ask if user was ready."
      correct_formatting = False
    elif "Guess 1" not in answer_lines[6] or \
         "Guess 2" not in answer_lines[8] or \
         "Guess 3" not in answer_lines[10]:
      formatting_mistake = "Didn't list guess numbers correctly."
      correct_formatting = False
    elif "Enter 1 if my guess was high, -1 if low, and 0 if correct:" not in answer_lines[7] or \
         "Enter 1 if my guess was high, -1 if low, and 0 if correct:" not in answer_lines[9] or \
         "Enter 1 if my guess was high, -1 if low, and 0 if correct:" not in answer_lines[11]:
      formatting_mistake = "Didn't promp user to enter 0, -1, or 1 correctly."
      correct_formatting = False
    elif "Thank you for playing the Guessing Game." not in answer_lines[-1]:
      formatting_mistake = "Did not correctly thank the user for playing."
      correct_formatting = False
    
    if not correct_formatting:
      print("\tIncorrect Formatting (-5)")
      print("\t=====Correct=====\n\t"+'\n\t'.join(correct_lines)+"\n\t=====Output=====\n\t"+'\n\t'.join(answer_lines))

    # gradin' normal tests
    numFailed = 0
    for i,(correct,out) in enumerate(zip(correctOutput,answers)):
      print("=====Test "+str(i+1)+"=====")
      # check correctness
      failed = False
      # Get numbers following the intro to the game.
      theirNums = [int(s) for s in out[out.find('Are you ready')+1:].split() if s.isdigit()]
      ourNums = [int(s) for s in correct[correct.find('Are you ready')+1:].split() if s.isdigit()]

      if i == 2:
        if "bye" not in out.lower(): 
          failed = True
      elif i == 3:
        if out.count("Enter 1 if my guess was high, -1 if low, and 0 if correct:") != 3:
          failed = True
      elif i == 4:
        if badResponse.lower()[:-1] not in out.lower(): 
          failed = True
      elif theirNums != ourNums:
        failed = True

      if failed:
        comments.append("Failed test " + str(i+1) + ": " +
                        testDescription[i] + " (-5)")
        numFailed += 1
        print("\tFailed test "+str(i+1) + ": -5")
        print("\t=====Correct=====\n\t"+'\n\t'.join(correct.split('\n'))+"\n\t=====Output=====\n\t"+'\n\t'.join(out.split('\n')))
      else:
        print("\tPassed")

    #take off for test that we had to ^C
    if len(answers) < 5:
      numFailed += 5 - len(answers)

    #calculating grade time
    total_off = (numFailed * -5)
    print()
    print("Failed " + str(numFailed))

    if not correct_formatting:
      total_off -= 5
      feedback = "Incorrect formatting: " + formatting_mistake + " (-5)"
      comments.append(feedback)
      print(feedback)
    grade += total_off
    print("Grade: "+str(grade)+"/70")

  #checking for header and style
  #os.system('vim ' + fileToGrade)
  input("Hit Enter to cat")
  print(subprocess.getoutput('cat ' + fileToGrade))
  headerInput = input("Header and comments? (y/n, hit enter for y): ")
  if headerInput == 'y' or headerInput == '' :
    header = True
  else :
    header = False
  style = input("Style/Other (Out of 30, hit enter for 30): ")
  gen_comments = input("General Comments?: ").rstrip().lstrip()
  gen_comments = gen_comments if len(gen_comments) is not 0 else "style"
  if not style.isdigit():
    style = 30
  else :
    style = int(style)
  if (gen_comments != "style" or style != 30):
    gen_comments += " (%+d)" % (style - 30)
    comments.append("%s" % gen_comments)
  
  #writing grade time!
  if late == -1:
    if writeToFile: outputFile.write('0\t More than 7 days late')
    print('Late more than 7 days!')
  else :
    if late == 3:
      comments.append("3 - 7 days late (-30)")
      grade -= 30
    elif late == 2 :
      comments.append("2 days late (-20)")
      grade -= 20
    elif late == 1 :
      comments.append("1 day late (-10)")
      grade -= 10
    
    if wrongFileName :
      comments.append("wrong filename (-10)")
      grade -= 10
    if not header :
      comments.append("missing comments or malformed header (-10)")
      grade -= 10

    if writeToFile: outputFile.write(str(grade+style) + "\t" + ', '.join(comments))
      
  if writeToFile: outputFile.write('\n')
  os.chdir("..")
Exemplo n.º 48
0
def get_python_helper_lib_filename():
    # Note: we have an independent (and similar -- but not equal) version of this method in
    # `add_code_to_python_process.py` which should be kept synchronized with this one (we do a copy
    # because the `pydevd_attach_to_process` is mostly independent and shouldn't be imported in the
    # debugger -- the only situation where it's imported is if the user actually does an attach to
    # process, through `attach_pydevd.py`, but this should usually be called from the IDE directly
    # and not from the debugger).
    libdir = os.path.join(os.path.dirname(__file__),
                          'pydevd_attach_to_process')

    arch = ''
    if IS_WINDOWS:
        # prefer not using platform.machine() when possible (it's a bit heavyweight as it may
        # spawn a subprocess).
        arch = os.environ.get("PROCESSOR_ARCHITEW6432",
                              os.environ.get('PROCESSOR_ARCHITECTURE', ''))

    if not arch:
        arch = platform.machine()
        if not arch:
            pydev_log.info('platform.machine() did not return valid value.'
                           )  # This shouldn't happen...
            return None

    if IS_WINDOWS:
        extension = '.dll'
        suffix_64 = 'amd64'
        suffix_32 = 'x86'

    elif IS_LINUX:
        extension = '.so'
        suffix_64 = 'amd64'
        suffix_32 = 'x86'

    elif IS_MAC:
        extension = '.dylib'
        suffix_64 = 'x86_64'
        suffix_32 = 'x86'

    else:
        pydev_log.info('Unable to set trace to all threads in platform: %s',
                       sys.platform)
        return None

    if arch.lower() not in ('amd64', 'x86', 'x86_64', 'i386', 'x86'):
        # We don't support this processor by default. Still, let's support the case where the
        # user manually compiled it himself with some heuristics.
        #
        # Ideally the user would provide a library in the format: "attach_<arch>.<extension>"
        # based on the way it's currently compiled -- see:
        # - windows/compile_windows.bat
        # - linux_and_mac/compile_linux.sh
        # - linux_and_mac/compile_mac.sh

        try:
            found = [
                name for name in os.listdir(libdir)
                if name.startswith('attach_') and name.endswith(extension)
            ]
        except:
            if DebugInfoHolder.DEBUG_TRACE_LEVEL >= 1:
                # There is no need to show this unless debug tracing is enabled.
                pydev_log.exception('Error listing dir: %s', libdir)
            return None

        expected_name = 'attach_' + arch + extension
        expected_name_linux = 'attach_linux_' + arch + extension

        filename = None
        if expected_name in found:  # Heuristic: user compiled with "attach_<arch>.<extension>"
            filename = os.path.join(libdir, expected_name)

        elif IS_LINUX and expected_name_linux in found:  # Heuristic: user compiled with "attach_linux_<arch>.<extension>"
            filename = os.path.join(libdir, expected_name_linux)

        elif len(
                found
        ) == 1:  # Heuristic: user removed all libraries and just left his own lib.
            filename = os.path.join(libdir, found[0])

        else:  # Heuristic: there's one additional library which doesn't seem to be our own. Find the odd one.
            filtered = [
                name for name in found
                if not name.endswith((suffix_64 + extension,
                                      suffix_32 + extension))
            ]
            if len(filtered
                   ) == 1:  # If more than one is available we can't be sure...
                filename = os.path.join(libdir, found[0])

        if filename is None:
            pydev_log.info(
                'Unable to set trace to all threads in arch: %s (did not find a %s lib in %s).',
                arch, expected_name, libdir)
            return None

        pydev_log.info('Using %s lib in arch: %s.', filename, arch)

    else:
        # Happy path for which we have pre-compiled binaries.
        if IS_64BIT_PROCESS:
            suffix = suffix_64
        else:
            suffix = suffix_32

        if IS_WINDOWS or IS_MAC:  # just the extension changes
            prefix = 'attach_'
        elif IS_LINUX:  #
            prefix = 'attach_linux_'  # historically it has a different name
        else:
            pydev_log.info(
                'Unable to set trace to all threads in platform: %s',
                sys.platform)
            return None

        filename = os.path.join(libdir, '%s%s%s' % (prefix, suffix, extension))

    if not os.path.exists(filename):
        pydev_log.critical('Expected: %s to exist.', filename)
        return None

    return filename
Exemplo n.º 49
0
def assign8( csid , writeToFile) :
  fileToGrade = ""
  late = 0
  grade = 70
  style = 30
  wrongFileName = False
  header = True
  comments = []

  os.chdir(csid)
  if writeToFile: outputFile.write(csid + "\t")
  files = os.listdir('.')

  #filename checking
  for f in files :
    splitted = subprocess.getoutput('ls -l ' + f).split()
    if f == filename :
      fileToGrade = filename
      late = isLate(splitted)
      break
    elif f == filename.lower() :
      fileToGrade = filename.lower()
      late = isLate(splitted)
      wrongFileName = True
      break

  #really odd filename
  if fileToGrade == "" :
    print(subprocess.getoutput('ls -l'))
    fileToGrade = input("Which file should I grade? ")
    if fileToGrade == "" :
      if writeToFile: 
        outputFile.write("0\tno file\n")
      os.chdir("..")
      return
    else :
      splitted = subprocess.getoutput('ls -l ' + fileToGrade.replace(' ','\ ')).split()
      late = isLate(splitted)
      wrongFileName = True

  #grading time!
  if not fileToGrade == "" and late != -1:
    correct_format_left = True
    correct_format_mid = True
    correct_format_right = True
    correct_format_other = True
    valid_output = True
    calculated_values = []

    # perfect match
    first_line = "Computation of PI using Random Numbers"
    last_line = "Difference = Calculated PI - math.pi"
    # Columns must line up, the calculated pi must have 6 decimal places, and
    # there must be a +/- in front of the difference. However, column spacing
    # can be off by +/- 1.
    regex_perfect = "^num = 100[0 ]{5}  [ ]{0,2}Calculated PI = \d\.\d{6}  [ ]{0,2}Difference = [+-]\d(?:\.\d{6}|e[+-]\d{5})$"
    regex_left = "100[\d\s]{5}"
    regex_mid = "=\s*[\d\.]{8}"
    regex_right = "[+-e\d\.]{9}$"

    # grab pi and difference
    regex_grab = "[+-]?\d*\.?\d+(?:[eE][+-]\d+)?"

    for run_num in range(averageNumRuns):
      process = subprocess.Popen(['python3', fileToGrade], stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
      try:
        out = process.communicate(bytes('50000', 'UTF-8'))[0]
      except KeyboardInterrupt:
        out = "\n\n\n\n"
      answer = list(line.strip() for line in str(out)[2:-1].replace('\\n', '\n').replace('\\t', '\t').strip().split('\n'))
      print('\n'.join(answer))

      # grab the difference
      try:
        raw_grabbed = list(filter(lambda match: len(match) > 0, [re.findall(regex_grab, line) for line in answer]))
        grabbed_pi = [float(pi) for (_, pi, _) in raw_grabbed]
        grabbed_diff = [float(diff) for (_, _, diff) in raw_grabbed]
        calculated_values.append(grabbed_diff)
      except:
        pass

      # perfect matching
      correct_format_other = correct_format_other and answer[0] == first_line and answer[-1] == last_line and len(answer) is 10
      try:
        perfect_matches = list(filter(lambda match: match is not None, [re.search(regex_perfect, line) for line in answer]))
        correct_format_other = correct_format_other and len(perfect_matches) is 6
      except:
        correct_format_other = False
      try:
        left_matches = list(filter(lambda match: match is not None, [re.search(regex_left, line) for line in answer]))
        correct_format_left = correct_format_left and len(left_matches) is 6
      except:
        correct_format_left = False
      try:
        mid_matches = list(filter(lambda match: match is not None, [re.search(regex_mid, line) for line in answer]))
        correct_format_mid = correct_format_mid and len(mid_matches) is 6
      except:
        correct_format_mid = False
      try:
        right_matches = list(filter(lambda match: match is not None, [re.search(regex_right, line) for line in answer]))
        correct_format_right = correct_format_right and len(right_matches) is 6
      except:
        correct_format_right = False

      # 0. verify decreasing error as n increases
      # 1. verify avg_diff + avg_pi is very close to math.pi
      # 2. verify avg_pi is within 0.5 of math.pi
      try:
        averages = list(map(lambda tup: sum(tup) / len(tup), zip(*[[math.fabs(x) for x in run] for run in calculated_values])))
        average_pi = [math.fabs(x) for x in grabbed_pi]
        average_pi = sum(average_pi) / len(average_pi)
        print("This Run: %s" % ', '.join(['%.6f' % math.fabs(x) for x in grabbed_diff]))
        print("All Runs: %s\n" % ', '.join(map(lambda i: '%.6f' % i, averages)))
        valid_output = valid_output and all([(math.fabs(calc - math.pi - diff) < 0.0001) for calc, diff in zip(grabbed_pi, grabbed_diff)])
        valid_output = valid_output and math.fabs(math.pi - average_pi) < 0.5
      except:
        valid_output = False

      # stops if either the current run or the average runs pass condition 0
      if all(x >= y for x,y in zip(averages, averages[1:])) or all(math.fabs(x) >= math.fabs(y) for x,y in zip(grabbed_diff, grabbed_diff[1:])):
        break

    if correct_format_left and correct_format_mid and correct_format_right and correct_format_other and valid_output:
      print("Perfect! ^_^")
    elif not (correct_format_left or correct_format_mid or correct_format_right or correct_format_other or valid_output):
      print("non-working code. ='(")
      comments.append("non-working code (-25)")
      grade -= 25
    else:
      if not correct_format_left:
        manual_override = input("Is num left justified? (y / (n or enter)): ")
        if manual_override != 'y':
          comments.append("num not left justified (-5)")
          grade -= 5
      if not correct_format_mid:
        manual_override = input("Does pi have 6 decimals and no sign in front? (y / (n or enter)): ")
        if manual_override != 'y':
          comments.append("pi not expressed to six decimals or with sign (-5)")
          grade -= 5
      if not correct_format_right:
        manual_override = input("Does diff have 6 decimals and a sign in front? (y / (n or enter)): ")
        if manual_override != 'y':
          comments.append("diff not expressed to six decimals or without sign (-5)")
          grade -= 5
      if not correct_format_other:
        manual_override = input("Is there spacing or other general misformatting " +
                                "not yet covered? This includes the case where " +
                                "they forgot to have the opening and ending lines. " +
                                "((y or enter) / n): ")
        if manual_override != 'n':
          explanation = input("Care to elaborate? ")
          if explanation == "": explanation = "spacing or other general misformatting in output"
          comments.append(explanation + " (-5)")
          grade -= 5
      if not valid_output:
        print("calculations are wrong (-5)")
        comments.append("diff is wrong sign or just wrong (-5)")
        grade -= 5

  #checking for header and style
  #os.system('vim ' + fileToGrade)
  input("Hit Enter to cat")
  print(subprocess.getoutput('cat ' + fileToGrade))
  headerInput = input("Header and comments? (y/n, hit enter for y): ")
  if headerInput == 'y' or headerInput == '' :
    header = True
  else :
    header = False
  style = input("Style/Other (Out of 30, hit enter for 30): ")
  gen_comments = input("General Comments?: ").strip()
  gen_comments = gen_comments if len(gen_comments) is not 0 else "code style looks fine"
  if not style.isdigit() or style == 30:
    style = 30
    gen_comments += " (-0)"
  else :
    style = int(style)
    gen_comments += " (%+d)" % (style - 30)
  comments.append("%s" % gen_comments)
  
  #writing grade time!
  if late == -1:
    if writeToFile: outputFile.write('0\t More than 7 days late')
    print('Late more than 7 days!')
  else :
    if late == 3:
      comments.append("3 - 7 days late (-30)")
      grade -= 30
    elif late == 2 :
      comments.append("2 days late (-20)")
      grade -= 20
    elif late == 1 :
      comments.append("1 day late (-10)")
      grade -= 10
    
    if wrongFileName :
      comments.append("wrong filename (-10)")
      grade -= 10
    if not header :
      comments.append("missing comments or malformed header (-10)")
      grade -= 10

    if writeToFile: outputFile.write(str(grade+style) + "\t" + ', '.join(comments))
      
  if writeToFile: outputFile.write('\n')
  os.chdir("..")
Exemplo n.º 50
0
def assign13(csid , writeToFile) :
  fileToGrade = ""
  late = 0
  grade = 70
  style = 30
  wrongFileName = False
  header = True
  comments = []

  os.chdir(csid)
  if writeToFile: outputFile.write(csid + "\t")
  files = os.listdir('.')

  #filename checking
  for f in files :
    splitted = subprocess.getoutput('ls -l ' + f).split()
    if f == filename :
      fileToGrade = filename
      late = isLate(splitted)
      break
    elif f == filename.lower() :
      fileToGrade = filename.lower()
      late = isLate(splitted)
      wrongFileName = True
      break

  #really odd filename
  if fileToGrade == "" :
    print(subprocess.getoutput('ls -l'))
    fileToGrade = input("Which file should I grade? ")
    if fileToGrade == "" :
      if writeToFile: 
        outputFile.write("0\tno file\n")
      os.chdir("..")
      return
    else :
      splitted = subprocess.getoutput('ls -l ' + fileToGrade.replace(' ','\ ')).split()
      late = isLate(splitted)
      wrongFileName = True


  # copies ../FILE_TO_COPY.txt to ./hidden.txt and runs the program in ./
  # diffs ./found.txt with ../FILE_TO_COPY_found.txt
  perfect_formatting = True
  def cp_run_and_diff (file_to_copy):
    nonlocal perfect_formatting
    os.chdir ('..')
    subprocess.getoutput('rm "%s/hidden.txt"' % csid)
    subprocess.getoutput('rm "%s/found.txt"' % csid)
    os.system('cp %s.txt "%s/hidden.txt"' % (file_to_copy, csid))
    os.chdir(csid)
    subprocess.getoutput('python3 %s' % fileToGrade)
    if file_to_copy != bonus_name:
      print ("Testing %s:" % file_to_copy)
    success = True
    try:
      with open ('found.txt', 'r') as actual, open ('../%s_found.txt' % file_to_copy, 'r') as expected:
        actual_lines = [x.strip() for x in list(actual) if len(x.strip()) != 0]
        actual_lines.sort()
        expected_lines = [x.strip() for x in list(expected) if len(x.strip()) != 0]
        expected_lines.sort()
        if (len(actual_lines) != len(expected_lines)):
          success = False
          if (file_to_copy == file_names[0]):
            perfect_formatting = False
        else:
          last_line = None
          for expected_line, actual_line in zip(expected_lines, actual_lines):
            if not success:
              break
            if file_to_copy == file_names[0] and last_line is not None:
              perfect_formatting = perfect_formatting and len(last_line) == len(actual_line)
            last_line = actual_line
            success = expected_line.split() == actual_line.split()
        if True or not success and file_to_copy != bonus_name:
          print ('  Expected:')
          for line in expected_lines:
            print ('    |%s' % line)
          print ('  Actual:')
          for line in actual_lines:
            print ('    |%s' % line)
          print()
        elif success:
          if file_to_copy == bonus_name:
            print ("Testing '%s':" % file_to_copy)
          print ('Program output matches exactly!\n')
    except:
      if file_to_copy != bonus_name:
        print ("Program did not output to file.\n")
      if (file_to_copy == file_names[0]):
        perfect_formatting = False
      return False

    subprocess.getoutput('rm hidden.txt')
    subprocess.getoutput('rm found.txt')
    return success

  if late != -1:
    test_results = []
    for test in file_names:
      test_results.append (cp_run_and_diff (test))
    test_results.append (cp_run_and_diff (bonus_name))
    
    if test_results[-1]:
      print("Bonus: Diagonal searching implemented correctly! (+10)\n")
      comments.append("passed diagonal bonus (+10)")
      grade += 10
    if perfect_formatting and any(test_results[:-2]):
      print("Bonus: formatting is perfect! (+5)\n")
      comments.append("passed formatting bonus (+5)")
      grade += 5
    if all(test_results[:-1]):
      print("Perfect! ^_^")
      comments.append("passed all tests")
    elif not any(test_results[:-1]):
      print("Failed every test... ='(")
      comments.append("failed all tests (-30)")
      grade -= 30
    else:
      first_fail = True
      def print_fail (string):
        nonlocal first_fail
        if first_fail:
          first_fail = False
          print(string)

      functionality_fail = "Tests failed (6 points each):"
      num_off = 0
      for i in range (len(test_results) - 1):
        if not test_results[i]:
          print_fail (functionality_fail)
          print("\t%s" % file_names[i])
          comments.append("failed %s (-6)" % file_names[i])
          num_off += 6
      grade -= num_off
      print("Total off: (-%d)" % num_off)

  #checking for header and style
  #os.system('vim ' + fileToGrade)
  input("Hit Enter to cat")
  print(subprocess.getoutput('cat ' + fileToGrade))
  headerInput = input("Header and comments? (y/n, hit enter for y): ")
  if headerInput == 'y' or headerInput == '' :
    header = True
  else :
    header = False
  style = input("Style/Other (Out of 30, hit enter for 30): ")
  gen_comments = input("General Comments?: ").rstrip().lstrip()
  gen_comments = gen_comments if len(gen_comments) is not 0 else "style"
  if not style.isdigit():
    style = 30
  else :
    style = int(style)
  if (gen_comments != "style" or style != 30):
    gen_comments += " (%+d)" % (style - 30)
    comments.append("%s" % gen_comments)
  
  #writing grade time!
  if late == -1:
    if writeToFile: outputFile.write('0\t More than 7 days late')
    print('Late more than 7 days!')
  else :
    if late == 3:
      comments.append("3 - 7 days late (-30)")
      grade -= 30
    elif late == 2 :
      comments.append("2 days late (-20)")
      grade -= 20
    elif late == 1 :
      comments.append("1 day late (-10)")
      grade -= 10
    
    if wrongFileName :
      comments.append("wrong filename (-10)")
      grade -= 10
    if not header :
      comments.append("missing comments or malformed header (-10)")
      grade -= 10

    if writeToFile: outputFile.write(str(grade+style) + "\t" + ', '.join(comments))
      
  if writeToFile: outputFile.write('\n')
  os.chdir("..")
Exemplo n.º 51
0
    # indexed_docs_path contain indexed docs
    docs_indexed = [
        f for f in listdir(indexed_docs_path)
        if isfile(join(indexed_docs_path, f))
    ]
    # print(docs_indexed)
    # exit(0)
    # extracting name of folder in f_index directory
    # f_index directory contain forward indices
    # if data set contain multiple files then f_index contain folder for each file and in it contains its forward_index
    dir_dict = {}

    for doc in docs:

        f_index_folder = [
            dI for dI in os.listdir(f_index_path)
            if os.path.isdir(os.path.join(f_index_path, dI))
        ]
        # print(f_index_folder)
        # exit(0)
        # reading doc_subdirectories file to get name of all files that are indexed
        dir_dict = read_doc_sub_directories()

        # if doc not in indexed directory then it needs to be indexed
        if doc not in dir_dict:
            # if f_index list is empty it means no folder in directory
            output_path, sub_d = get_out_path_for_f_index(doc, f_index_folder)

            # docs_path_n is the path of nth doc in docs_path directory
            docs_path_n = docs_path + "\\" + str(doc)
def assign12(csid , writeToFile) :
  fileToGrade = ""
  late = 0
  grade = 70
  style = 30
  wrongFileName = False
  header = True
  comments = []

  os.chdir(csid)
  if writeToFile: outputFile.write(csid + "\t")
  files = os.listdir('.')

  #filename checking
  for f in files :
    splitted = subprocess.getoutput('ls -l ' + f).split()
    if f == filename :
      fileToGrade = filename
      late = isLate(splitted)
      break
    elif f == filename.lower() :
      fileToGrade = filename.lower()
      late = isLate(splitted)
      wrongFileName = True
      break

  #really odd filename
  if fileToGrade == "" :
    print(subprocess.getoutput('ls -l'))
    fileToGrade = input("Which file should I grade? ")
    if fileToGrade == "" :
      if writeToFile: 
        outputFile.write("0\tno file\n")
      os.chdir("..")
      return
    else :
      splitted = subprocess.getoutput('ls -l ' + fileToGrade.replace(' ','\ ')).split()
      late = isLate(splitted)
      wrongFileName = True

  # run student's isbn.py and compare their output with standard output
  def run_program_compare():
    os.chdir('..')
    os.system('cp %s %s/isbn.txt' % (input_file, csid))
    os.chdir(csid)
    command = 'python3 ' + fileToGrade
    os.system( command )
    wrongCases = testCases
    count = 1
    try:
      user_file = open(output_file, 'r')
      user_line = user_file.readline()
      std_file = open('../%s' % output_file, 'r')
      std_line = std_file.readline()
      
      while (std_line and user_line ):
        print ("test case", count)
        print ("Expected: ", std_line, end='')
        print ("Actual:   ", user_line)
        count += 1
        if std_line.lower().split() == user_line.lower().split():
          wrongCases -= 1
        elif std_line.lower().split()[1] in user_line.lower().split():
          wrongCases -= 0.5
        user_line = user_file.readline()
        std_line = std_file.readline()

      while(std_line):
          print ("test case", count)
          print ("Expected: ", std_line, end='')
          print ("Actual:   ")
          user_line = user_line.readline()
          count += 1
      while(user_line):
          wrongCases += 1
          print ("Expected: ", end='')
          print ("Actual:   ", user_line)
          user_line = user_line.readline()
    except:
      pass
    if wrongCases <= testCases: return wrongCases
    else: return testCases

  if late != -1:
    wrongTests = run_program_compare()
    print ("correct: ", testCases - wrongTests)
    print ("wrong:   ", wrongTests)
    if wrongTests == 0:
      print("Excellent! passed all tests")
      comments.append("passed all tests")
    else:
      grade = grade - (30/testCases) * wrongTests
      c = str(wrongTests) + " are wrong"
      comments.append(c)

  #checking for header and style
  #os.system('vim ' + fileToGrade)
  input("Hit Enter to cat")
  print(subprocess.getoutput('cat ' + fileToGrade))
  headerInput = input("Header and comments? (y/n, hit enter for y): ")
  if headerInput == 'y' or headerInput == '' :
    header = True
  else :
    header = False
  style = input("Style/Other (Out of 30, hit enter for 30): ")
  gen_comments = input("General Comments?: ").rstrip().lstrip()
  gen_comments = gen_comments if len(gen_comments) is not 0 else "style"
  if not style.isdigit():
    style = 30
  else :
    style = int(style)
  if (gen_comments != "style" or style != 30):
    gen_comments += " (%+d)" % (style - 30)
    comments.append("%s" % gen_comments)
  
  #writing grade time!
  if late == -1:
    if writeToFile: outputFile.write('0\t More than 7 days late')
    print('Late more than 7 days!')
  else :
    if late == 3:
      comments.append("3 - 7 days late (-30)")
      grade -= 30
    elif late == 2 :
      comments.append("2 days late (-20)")
      grade -= 20
    elif late == 1 :
      comments.append("1 day late (-10)")
      grade -= 10
    
    if wrongFileName :
      comments.append("wrong filename (-10)")
      grade -= 10
    if not header :
      comments.append("missing comments or malformed header (-10)")
      grade -= 10

    if writeToFile: outputFile.write(str(grade+style) + "\t" + ', '.join(comments))
      
  if writeToFile: outputFile.write('\n')
  os.chdir("..")
Exemplo n.º 53
0
def _dirstats(dpath=None):  # nocover
    """
    Testing helper for printing directory information
    (mostly for investigating windows weirdness)
    """
    from ubelt import util_colors
    if dpath is None:
        dpath = os.getcwd()
    print('===============')
    print('Listing for dpath={}'.format(dpath))
    print('E L F D J - path')
    print('--------------')
    if not os.path.exists(dpath):
        print('... does not exist')
        return
    paths = sorted(os.listdir(dpath))
    for path in paths:
        full_path = join(dpath, path)
        E = os.path.exists(full_path)
        L = os.path.islink(full_path)
        F = os.path.isfile(full_path)
        D = os.path.isdir(full_path)
        J = util_platform.WIN32 and _win32_links._win32_is_junction(full_path)
        ELFDJ = [E, L, F, D, J]
        if ELFDJ == [1, 0, 0, 1, 0]:
            # A directory
            path = util_colors.color_text(path, 'green')
        elif ELFDJ == [1, 0, 1, 0, 0]:
            # A file (or a hard link they are indistinguishable with one query)
            path = util_colors.color_text(path, 'white')
        elif ELFDJ == [1, 0, 0, 1, 1]:
            # A directory junction
            path = util_colors.color_text(path, 'yellow')
        elif ELFDJ == [1, 1, 1, 0, 0]:
            # A file link
            path = util_colors.color_text(path, 'brightgreen')
        elif ELFDJ == [1, 1, 0, 1, 0]:
            # A directory link
            path = util_colors.color_text(path, 'brightcyan')
        elif ELFDJ == [0, 1, 0, 0, 0]:
            # A broken file link
            path = util_colors.color_text(path, 'red')
        elif ELFDJ == [0, 1, 0, 1, 0]:
            # A broken directory link
            path = util_colors.color_text(path, 'darkred')
        elif ELFDJ == [0, 0, 0, 1, 1]:
            # A broken directory junction
            path = util_colors.color_text(path, 'purple')
        elif ELFDJ == [1, 0, 1, 0, 1]:
            # A file junction? Thats not good.
            # I guess this is a windows 7 thing?
            path = util_colors.color_text(path, 'red')
        elif ELFDJ == [1, 1, 0, 0, 0]:
            # Windows? Why? What does this mean!?
            # A directory link that cant be resolved?
            path = util_colors.color_text(path, 'red')
        else:
            print('dpath = {!r}'.format(dpath))
            print('path = {!r}'.format(path))
            raise AssertionError(str(ELFDJ) + str(path))
        line = '{E:d} {L:d} {F:d} {D:d} {J:d} - {path}'.format(**locals())
        if os.path.islink(full_path):
            line += ' -> ' + os.readlink(full_path)
        elif _win32_links is not None:
            if _win32_links._win32_is_junction(full_path):
                line += ' => ' + _win32_links._win32_read_junction(full_path)
        print(line)
Exemplo n.º 54
0
def extract_step(text_path: str = paths.TEXT_PATH,
                 language="english",
                 texts: [str] = None,
                 alias=None):
    """
    Processes all files in a given folder.
    The process is as follows:
        1) Read the content of the file
        2) Split the content into Paragraph objects (each Paragraph has a list
            of containing sentences)
        3) For each paragraph:
            3.1) POS-tag the sentences
            3.2) Map the tokens to the sentence-position in the paragraph
                Example:
                    Paragraph:
                        Paul threw the red ball. The ball landed on the roof.
                    Sentences:
                        [Paul threw the red ball., The ball landed on the roof.]
                    Tokens:
                        [Paul, threw, the, red, ball, landed, on, roof]
                    Mapping:
                        [Paul: (0), threw: (0), the: (0, 1), ...]
            3.3) Generate the "Closeness" for each combination of tokens
                The weighting of the "Closeness" is calculated like so:
                    - For each combination of tokens, check all combinations
                        of positions.
                    - Check the distance (math.abs()) of the distances
                    - If it is below the threshold (default 5), add
                        the following to the current weight:
                            1/(1 + distance)
                    - If it is above the threshold, continue with the next
                        position
                    - After all positional combinations are checked, create
                        a "Closeness" object for the two tokens, with the weight
                        being the value that was calculated as above IF the
                        weight is above the threshold (1/3). If it is not,
                        continue with the next token.

                        In the example above the weight would be 1.5, so a
                        new Closeness object would be created and added to the
                        list.
            3.4) Add the resulting list from 3.3) to a list and continue with
                the next paragraph
        4) Write the resulting list of "Closeness" objects for each text
            to the disk.
    :param text_path: The path to the folder where the files are.
    :param language: Optional. Default: "english". The language of the texts.
    :param texts: Optional. A list of strings of filenames to be processed. If not provided, all texts in the system
        will be used.
    :param alias: Optional. The Alias of the collection of texts.
    """
    # iterate over texts
    closeness = []
    files = os.listdir(text_path)
    if texts:
        files = texts
    for text in files:
        if not text.endswith(".txt"):
            # support different file types here
            continue
        with (open(paths.TEXT_PATH + "/" + text, "r",
                   encoding="utf-8")) as current_text:
            text_content = current_text.read()
            # split the text into paragraphs first
            paragraphs = split_paragraphs(text_content)
            # take each paragraph, pos tag each paragraph content
            paragraph_list = []
            with open(paths.TEXT_META_PATH + "/{}_meta".format(text),
                      "w",
                      encoding="utf8") as metafile:
                metafile.write("PARAGRAPHS: {}".format(len(paragraphs)))
            print("Current text: {}".format(text))
            bar = progressbar.ProgressBar(max_value=len(paragraphs))
            for count, paragraph in enumerate(paragraphs):
                bar.update(count)
                # make new paragraph, with pos-tagged sentence list
                pos = pos_tag(paragraph)
                new_paragraph = Paragraph(count, pos, text)
                paragraph_list.append(new_paragraph)
                # {'study': {0}, 'temperament': {0}}
                text2sentence = extract_from_sentences(new_paragraph.sentences,
                                                       language)
                # terms = text2sentence.keys()
                closeness_list = calculate_weighted_distance(
                    text2sentence,
                    paragraph_id="{}_{}".format(str(text), str(count)))
                closeness.append(closeness_list)
                with open(paths.PARAGRAPH_CONTENT_PATH + "{}/{}_{}".format(alias, text, count), "w", encoding="utf8") \
                        as content_file:
                    content_file.write(paragraph)
            pickle.dump(
                paragraph_list,
                open(paths.POS_PATH + "/" + alias + "/" + text + ".p", "wb"))

    with open(paths.TEXT_META_PATH + "/all_meta", "w",
              encoding="utf8") as metafile:
        metafile.write(",".join([x for x in files if x.endswith(".txt")]))
    pickle.dump(
        closeness,
        open(paths.CLOSENESS_PATH + "/" + alias + "/" + "closeness.p", "wb"))
Exemplo n.º 55
0
# coding=utf-8
import re
from os.path import os
from xlwt.Workbook import Workbook

mypath = 'D:/alltxt/'
myfiles = os.listdir(mypath)
fileList = []
excellist = []
for f in myfiles:
    if (os.path.isfile(mypath + '/' + f)):
        if os.path.splitext(f)[1] == '.txt':
            fileList.append(f)
        # 添加文件
for ff in fileList:
    f = open(mypath + ff, 'r', encoding='utf-8')
    #f = open('D:/alltxt/sndkPicklist_20180102170314_596_359508798.txt', 'r',encoding='utf-8')
    sourceInLines = f.readlines()
    f.close()
    #d = {}
    for line in sourceInLines:
        lists = []
        temp1 = line.strip('\n')
        temp2 = temp1.split('|')
        #matchDE = re.match( '^DE', temp1)
        #matchOP = re.match('^OP',temp1)
        #matchWO = re.match('^WO',temp1)
        if re.match('^WO', temp1):
            #temp2 = temp1.split('|')
            wo = temp2[2]
            sdpn = temp2[3]
Exemplo n.º 56
0
        if word not in stopwords:
            if word != '\t':
                outstr += word
                outstr += ' '
    print('outstr:{}'.format(outstr))
    return outstr


def stopwordslist():
    stopwords = [line.strip() for line in open(
        'baidu_stopwords.txt', encoding='UTF-8').readlines()]
    return stopwords


mypath = 'F:\\蔡觐阳\\Programming\\Python\\MachineLearning\\nlp\\pos\\'
pathlist = os.listdir(mypath)
# print(pathlist)


fileList = []
for f in pathlist:
    if(os.path.isfile(mypath + '/' + f)):
        if os.path.splitext(f)[1] == '.txt':
            fileList.append(f)
print(fileList)


excellist = []
for ff in fileList:
    f = open(mypath+ff, 'r', encoding='utf8', errors='ignore')
    sourceInLines = f.readlines()