Conditions | 7 |
Total Lines | 62 |
Code Lines | 31 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | # -*- coding: utf-8 -*- |
||
55 | def __init__( |
||
56 | self, |
||
57 | jars: Optional[str] = None, |
||
58 | jvm_started: Optional[bool] = False, |
||
59 | mark_time_ranges: Optional[bool] = False, |
||
60 | include_range: Optional[bool] = False, |
||
61 | jvm_flags: Optional[List[str]] = None, |
||
62 | language: Optional[str] = 'english', |
||
63 | ): |
||
64 | """Initialize `SUTime` wrapper. |
||
65 | |||
66 | Args: |
||
67 | jars (Optional[str]): Path to previously downloaded SUTime Java |
||
68 | dependencies. Defaults to False. |
||
69 | jvm_started (Optional[bool]): Flag to indicate that JVM has been |
||
70 | already started (with all Java dependencies loaded). Defaults |
||
71 | to False. |
||
72 | mark_time_ranges (Optional[bool]): SUTime flag for |
||
73 | sutime.markTimeRanges. Defaults to False. |
||
74 | "Whether or not to recognize time ranges such as 'July to |
||
75 | August'" |
||
76 | include_range (Optional[bool]): SUTime flag for |
||
77 | sutime.includeRange. Defaults to False. |
||
78 | "Whether or not to add range info to the TIMEX3 object" |
||
79 | jvm_flags (Optional[List[str]]): List of flags passed to JVM. For |
||
80 | example, this may be used to specify the maximum heap size |
||
81 | using '-Xmx'. Has no effect if `jvm_started` is set to True. |
||
82 | Defaults to None. |
||
83 | language (Optional[str]): Selected language. Currently supported |
||
84 | are: english (/en), british, spanish (/es). Defaults to |
||
85 | `english`. |
||
86 | """ |
||
87 | self.mark_time_ranges = mark_time_ranges |
||
88 | self.include_range = include_range |
||
89 | self._is_loaded = False |
||
90 | self._lock = threading.Lock() |
||
91 | module_root = Path(__file__).resolve().parent |
||
92 | self.jars = Path(jars) if jars else module_root / 'jars' |
||
93 | |||
94 | self._check_language_model_dependency( |
||
95 | language.lower() if language else '', |
||
96 | ) |
||
97 | |||
98 | if not jvm_started: |
||
99 | self._classpath = self._create_classpath() |
||
100 | self._start_jvm(jvm_flags) |
||
101 | |||
102 | try: |
||
103 | # make it thread-safe |
||
104 | if threading.active_count() > 1: |
||
105 | if not jpype.isThreadAttachedToJVM(): |
||
106 | jpype.attachThreadToJVM() |
||
107 | self._lock.acquire() |
||
108 | wrapper = jpype.JClass(self._sutime_java_class) |
||
109 | self._sutime = wrapper( |
||
110 | self.mark_time_ranges, self.include_range, language, |
||
111 | ) |
||
112 | self._is_loaded = True |
||
113 | except Exception as exc: |
||
114 | sys.exit('Could not load JVM: {0}'.format(exc)) |
||
115 | finally: |
||
116 | self._lock.release() |
||
117 | |||
206 |