|
1
|
|
|
#! /usr/bin/env python |
|
2
|
|
|
# |
|
3
|
|
|
# Copyright (C) 2016 Rich Lewis <[email protected]> |
|
4
|
|
|
# License: 3-clause BSD |
|
5
|
|
|
|
|
6
|
1 |
|
""" |
|
7
|
|
|
# skchem.utils.io |
|
8
|
|
|
|
|
9
|
|
|
IO helper functions for skchem. |
|
10
|
|
|
""" |
|
11
|
|
|
|
|
12
|
|
|
import yaml |
|
|
|
|
|
|
13
|
1 |
|
import json |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
def line_count(filename): |
|
17
|
|
|
|
|
18
|
|
|
""" Quickly count the number of lines in a file. |
|
19
|
|
|
|
|
20
|
|
|
Adapted from http://stackoverflow.com/questions/845058/how-to-get-line-count-cheaply-in-python |
|
21
|
|
|
|
|
22
|
|
|
Args: |
|
23
|
|
|
filename (str): |
|
24
|
|
|
The name of the file to count for. |
|
25
|
|
|
|
|
26
|
|
|
""" |
|
27
|
|
|
|
|
28
|
|
|
f = open(filename, 'rb') |
|
|
|
|
|
|
29
|
|
|
lines = 0 |
|
30
|
|
|
buf_size = 1024 * 1024 |
|
31
|
|
|
read_f = f.read |
|
32
|
|
|
buf = read_f(buf_size) |
|
33
|
|
|
while buf: |
|
34
|
|
|
lines += buf.count(b'\n') |
|
35
|
|
|
buf = read_f(buf_size) |
|
36
|
1 |
|
return lines |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
def sdf_count(filename): |
|
40
|
|
|
|
|
41
|
|
|
""" Efficiently count molecules in an sdf file. |
|
42
|
|
|
|
|
43
|
|
|
Specifically, the function counts the number of times '$$$$' occurs at the |
|
44
|
|
|
start of lines in the file. |
|
45
|
|
|
|
|
46
|
|
|
Args: |
|
47
|
|
|
filename (str): The filename of the sdf file. |
|
48
|
|
|
|
|
49
|
|
|
Returns: |
|
50
|
|
|
int: the number of molecules in the file. |
|
51
|
|
|
""" |
|
52
|
|
|
|
|
53
|
|
|
with open(filename, 'rb') as f: |
|
|
|
|
|
|
54
|
|
|
return sum(1 for l in f if l[:4] == b'$$$$') |
|
55
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
def json_dump(obj, target=None): |
|
58
|
|
|
""" Write object as json to file or stream, or return as string. """ |
|
59
|
|
|
|
|
60
|
|
|
if target is None: |
|
61
|
|
|
return json.dumps(obj) |
|
62
|
|
|
elif isinstance(target, str): |
|
63
|
|
|
with open(target, 'w') as f: |
|
|
|
|
|
|
64
|
|
|
json.dump(obj, f) |
|
65
|
|
|
else: |
|
66
|
|
|
json.dump(obj, target) |
|
67
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
def yaml_dump(obj, target=None): |
|
70
|
|
|
""" Write object as yaml to file or stream, or return as string. """ |
|
71
|
|
|
|
|
72
|
|
|
if isinstance(target, str): |
|
73
|
|
|
with open(target, 'w') as f: |
|
|
|
|
|
|
74
|
|
|
yaml.dump(obj, f, default_flow_style=False) |
|
75
|
|
|
else: |
|
76
|
|
|
return yaml.dump(obj, target, default_flow_style=False) |
|
77
|
|
|
|
This can be caused by one of the following:
1. Missing Dependencies
This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.
2. Missing __init__.py files
This error could also result from missing
__init__.pyfiles in your module folders. Make sure that you place one file in each sub-folder.