chemistry_paradise 1.1.26
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of chemistry_paradise might be problematic. Click here for more details.
- checksums.yaml +7 -0
- data/README.md +419 -0
- data/bin/chemistry_paradise +7 -0
- data/chemistry_paradise.gemspec +46 -0
- data/doc/BUGS.md +16 -0
- data/doc/README.gen +391 -0
- data/doc/todo/TODO.md +26 -0
- data/lib/chemistry_paradise/base/base.rb +101 -0
- data/lib/chemistry_paradise/base/colours.rb +65 -0
- data/lib/chemistry_paradise/commandline/help.rb +40 -0
- data/lib/chemistry_paradise/commandline/menu.rb +88 -0
- data/lib/chemistry_paradise/commandline/parse_commandline.rb +94 -0
- data/lib/chemistry_paradise/constants/constants.rb +52 -0
- data/lib/chemistry_paradise/constants/file_constants.rb +33 -0
- data/lib/chemistry_paradise/constants/german_names_of_elements_to_element_symbol.rb +157 -0
- data/lib/chemistry_paradise/converters/celsius_to_fahrenheit.rb +143 -0
- data/lib/chemistry_paradise/converters/fahrenheit_to_celsius.rb +132 -0
- data/lib/chemistry_paradise/converters/shared.rb +15 -0
- data/lib/chemistry_paradise/gui/gtk3/calculate_molecular_weight/calculate_molecular_weight.rb +34 -0
- data/lib/chemistry_paradise/gui/gtk3/show_periodic_table/show_periodic_table.rb +34 -0
- data/lib/chemistry_paradise/gui/gtk3/temperature_converter/temperature_converter.rb +249 -0
- data/lib/chemistry_paradise/gui/shared_code/calculate_molecular_weight/calculate_molecular_weight_module.rb +171 -0
- data/lib/chemistry_paradise/gui/shared_code/show_periodic_table/show_periodic_table_module.rb +318 -0
- data/lib/chemistry_paradise/images/show_periodic_table.png +0 -0
- data/lib/chemistry_paradise/interactive_chemistry_shell.rb +246 -0
- data/lib/chemistry_paradise/project/project.rb +24 -0
- data/lib/chemistry_paradise/requires/common_external_requires.rb +17 -0
- data/lib/chemistry_paradise/requires/require_the_project.rb +19 -0
- data/lib/chemistry_paradise/shared.rb +130 -0
- data/lib/chemistry_paradise/show_electron_configuration.rb +250 -0
- data/lib/chemistry_paradise/show_element.rb +145 -0
- data/lib/chemistry_paradise/sinatra/sinatra.rb +131 -0
- data/lib/chemistry_paradise/split_molecule_names.rb +228 -0
- data/lib/chemistry_paradise/toplevel_methods/atomgewichte.rb +31 -0
- data/lib/chemistry_paradise/toplevel_methods/convert_parens.rb +64 -0
- data/lib/chemistry_paradise/toplevel_methods/display_where_the_molmasses_are_kept.rb +24 -0
- data/lib/chemistry_paradise/toplevel_methods/e.rb +16 -0
- data/lib/chemistry_paradise/toplevel_methods/kelvin.rb +34 -0
- data/lib/chemistry_paradise/toplevel_methods/language.rb +50 -0
- data/lib/chemistry_paradise/toplevel_methods/misc.rb +128 -0
- data/lib/chemistry_paradise/toplevel_methods/periodic_table.rb +16 -0
- data/lib/chemistry_paradise/toplevel_methods/remove_this_molecule_from.rb +63 -0
- data/lib/chemistry_paradise/toplevel_methods/show_electron_negativity_chart.rb +26 -0
- data/lib/chemistry_paradise/utility_scripts/calculate_atomic_mass.rb +559 -0
- data/lib/chemistry_paradise/utility_scripts/combustion_analysis.rb +207 -0
- data/lib/chemistry_paradise/utility_scripts/electron_negativity_chart.rb +78 -0
- data/lib/chemistry_paradise/utility_scripts/equalize_chemical_formula.rb +84 -0
- data/lib/chemistry_paradise/utility_scripts/equation_solver.rb +130 -0
- data/lib/chemistry_paradise/utility_scripts/orbitals.rb +70 -0
- data/lib/chemistry_paradise/utility_scripts/show_electron_negativity_of_this_element.rb +103 -0
- data/lib/chemistry_paradise/verbose_chemical_calculation.rb +21 -0
- data/lib/chemistry_paradise/version/version.rb +19 -0
- data/lib/chemistry_paradise/www/chemistry/chemistry.cgi +7 -0
- data/lib/chemistry_paradise/www/chemistry/chemistry.rb +1526 -0
- data/lib/chemistry_paradise/www/chemistry/chemistry.sinatra +56 -0
- data/lib/chemistry_paradise/www/organic_chemistry/organic_chemistry.md +16 -0
- data/lib/chemistry_paradise/yaml/atomgewichte.yml +113 -0
- data/lib/chemistry_paradise/yaml/colours_for_the_elements.yml +13 -0
- data/lib/chemistry_paradise/yaml/dichte.yml +21 -0
- data/lib/chemistry_paradise/yaml/electron_negativity_chart.yml +111 -0
- data/lib/chemistry_paradise/yaml/molecular_formula_of_different_molecules.yml +13 -0
- data/lib/chemistry_paradise/yaml/periodic_table_of_the_elements.yml +125 -0
- data/lib/chemistry_paradise.rb +1 -0
- data/test/testing_chemistry_paradise.rb +49 -0
- metadata +155 -0
@@ -0,0 +1,130 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
# Encoding: UTF-8
|
3
|
+
# frozen_string_literal: true
|
4
|
+
# =========================================================================== #
|
5
|
+
# === ChemistryParadise::Shared
|
6
|
+
#
|
7
|
+
# To load this file, do:
|
8
|
+
#
|
9
|
+
# require 'chemistry_paradise/shared.rb'
|
10
|
+
#
|
11
|
+
# =========================================================================== #
|
12
|
+
require 'chemistry_paradise/requires/common_external_requires.rb'
|
13
|
+
|
14
|
+
module ChemistryParadise # include ChemistryParadise::Shared
|
15
|
+
|
16
|
+
module Shared # === ChemistryParadise::Shared
|
17
|
+
|
18
|
+
require 'chemistry_paradise/constants/constants.rb'
|
19
|
+
require 'chemistry_paradise/version/version.rb'
|
20
|
+
require 'chemistry_paradise/toplevel_methods/convert_parens.rb'
|
21
|
+
require 'chemistry_paradise/toplevel_methods/periodic_table.rb'
|
22
|
+
|
23
|
+
include ChemistryParadise::Constants
|
24
|
+
|
25
|
+
# ========================================================================= #
|
26
|
+
# === ARRAY_TEST_THESE_MOLECULES
|
27
|
+
#
|
28
|
+
# Test these molecules. Default values.
|
29
|
+
# ========================================================================= #
|
30
|
+
ARRAY_TEST_THESE_MOLECULES = %w(
|
31
|
+
NH3
|
32
|
+
NH4
|
33
|
+
MnO2
|
34
|
+
P2O5
|
35
|
+
H2O
|
36
|
+
Pb3O4
|
37
|
+
H2SO4
|
38
|
+
C12H12N2
|
39
|
+
BaCO3
|
40
|
+
PbCrO4
|
41
|
+
Al2(SO4)3
|
42
|
+
Ca(OH)2
|
43
|
+
CH3COOH
|
44
|
+
C12H22O11
|
45
|
+
)
|
46
|
+
|
47
|
+
# ========================================================================= #
|
48
|
+
# === is_number?
|
49
|
+
#
|
50
|
+
# This method will return true if is a number, else false. For this it
|
51
|
+
# uses the .to_i method trick, which returns 0 for non-numbers.
|
52
|
+
# ========================================================================= #
|
53
|
+
def is_number?(i)
|
54
|
+
result = (i.to_i.to_s == i)
|
55
|
+
return result
|
56
|
+
end
|
57
|
+
|
58
|
+
# ========================================================================= #
|
59
|
+
# === square
|
60
|
+
#
|
61
|
+
# Die Wurzel aus 2 ist 1.414
|
62
|
+
#
|
63
|
+
# chem; square 2; sqr 3; sqr 4; sqr 5; sqr 6; sqr 7; sqr 8; sqr 9
|
64
|
+
#
|
65
|
+
# ========================================================================= #
|
66
|
+
def square(of)
|
67
|
+
Math.sqrt of
|
68
|
+
end; alias sqr square # === sqr
|
69
|
+
|
70
|
+
# ========================================================================= #
|
71
|
+
# === periodic_table?
|
72
|
+
# ========================================================================= #
|
73
|
+
def periodic_table?
|
74
|
+
return Shared.periodic_table?
|
75
|
+
end
|
76
|
+
|
77
|
+
# ========================================================================= #
|
78
|
+
# === ChemistryParadise::Shared.periodic_table?
|
79
|
+
# ========================================================================= #
|
80
|
+
def self.periodic_table?
|
81
|
+
::ChemistryParadise.periodic_table?
|
82
|
+
end
|
83
|
+
|
84
|
+
# ========================================================================= #
|
85
|
+
# === return_range_for_this_period
|
86
|
+
#
|
87
|
+
# This method will tell us the legal range of elements for any given
|
88
|
+
# period.
|
89
|
+
#
|
90
|
+
# The formula would be:
|
91
|
+
#
|
92
|
+
# 2+4n
|
93
|
+
#
|
94
|
+
# For instance:
|
95
|
+
# period 1 will return (1..2).
|
96
|
+
# period 2 will return (3..8).
|
97
|
+
# ========================================================================= #
|
98
|
+
def return_range_for_this_period(this_period = 1)
|
99
|
+
case this_period
|
100
|
+
when 1
|
101
|
+
(1..2)
|
102
|
+
when 2
|
103
|
+
(3..10)
|
104
|
+
when 3
|
105
|
+
(11..18)
|
106
|
+
when 4
|
107
|
+
(19..36)
|
108
|
+
when 5
|
109
|
+
(37..54)
|
110
|
+
when 6
|
111
|
+
(55..86)
|
112
|
+
when 7
|
113
|
+
(87..118)
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
# ========================================================================= #
|
118
|
+
# === convert_parens
|
119
|
+
# ========================================================================= #
|
120
|
+
def convert_parens(i)
|
121
|
+
ChemistryParadise.convert_parens(i)
|
122
|
+
end; alias parse convert_parens # === parse
|
123
|
+
|
124
|
+
end; end
|
125
|
+
|
126
|
+
if __FILE__ == $PROGRAM_NAME
|
127
|
+
include ChemistryParadise::Shared
|
128
|
+
pp convert_parens 'Al2(SO4)3'
|
129
|
+
pp convert_parens 'Fe(OH)3 + H2SO4 -> Fe2(SO4)3 + H2O'
|
130
|
+
end # rb shared.rb
|
@@ -0,0 +1,250 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
# Encoding: UTF-8
|
3
|
+
# frozen_string_literal: true
|
4
|
+
# =========================================================================== #
|
5
|
+
# === ChemistryParadise::ShowElectronConfiguration
|
6
|
+
#
|
7
|
+
# Feed this class an element as input, such as Ca or Fe, and it will
|
8
|
+
# display the electron configuration of said element.
|
9
|
+
#
|
10
|
+
# Usage examples:
|
11
|
+
#
|
12
|
+
# ChemistryParadise::ShowElectronConfiguration.new(ARGV)
|
13
|
+
#
|
14
|
+
# =========================================================================== #
|
15
|
+
# require 'chemistry_paradise/show_electron_configuration.rb'
|
16
|
+
# =========================================================================== #
|
17
|
+
require 'chemistry_paradise/base/base.rb'
|
18
|
+
|
19
|
+
module ChemistryParadise
|
20
|
+
|
21
|
+
class ShowElectronConfiguration < Base # === ChemistryParadise::ShowElectronConfiguration
|
22
|
+
|
23
|
+
# ========================================================================= #
|
24
|
+
# === DEFAULT_INPUT
|
25
|
+
# ========================================================================= #
|
26
|
+
DEFAULT_INPUT = 'Ca'
|
27
|
+
|
28
|
+
# ========================================================================= #
|
29
|
+
# === NEBENQUANTENZAHL
|
30
|
+
# ========================================================================= #
|
31
|
+
NEBENQUANTENZAHL = {
|
32
|
+
's' => 2,
|
33
|
+
'p' => 6,
|
34
|
+
'd' => 10,
|
35
|
+
'f' => 14
|
36
|
+
}
|
37
|
+
|
38
|
+
# ========================================================================= #
|
39
|
+
# 1. Periode: 1s
|
40
|
+
# 2. Periode: 2s 2p
|
41
|
+
# 3. Periode: 3s 3p
|
42
|
+
# 4. Periode: 4s 3d 4p
|
43
|
+
# 5. Periode: 5s 4d 5p
|
44
|
+
# 6. Periode: 6s 4f 5d 6p
|
45
|
+
# 7. Periode: 7s 5f 6d
|
46
|
+
# ========================================================================= #
|
47
|
+
# The following hash was taken from:
|
48
|
+
#
|
49
|
+
# https://en.wikipedia.org/wiki/Electron_configuration#Other_exceptions_to_Madelung.27s_rule
|
50
|
+
#
|
51
|
+
# and from:
|
52
|
+
#
|
53
|
+
# https://en.wikipedia.org/wiki/Electron_configurations_of_the_elements_%28data_page%29
|
54
|
+
#
|
55
|
+
# ========================================================================= #
|
56
|
+
MAIN_HASH = {
|
57
|
+
1 => '1s¹',
|
58
|
+
2 => '1s²',
|
59
|
+
3 => '1s² 2s¹',
|
60
|
+
4 => '1s² 2s²',
|
61
|
+
5 => '1s² 2s² 2p¹', # (5) Bor
|
62
|
+
6 => '1s² 2s² 2p²', # (6) Kohlenstoff
|
63
|
+
7 => '1s² 2s² 2p³', # (7) Stickstoff
|
64
|
+
8 => '1s² 2s² 2p⁴', # (8) Sauerstoff
|
65
|
+
9 => '1s² 2s² 2p⁵', # (9) Fluor
|
66
|
+
10 => '1s² 2s² 2p⁶', # (10) Neon
|
67
|
+
11 => '1s² 2s² 2p⁶ 3s¹', # (11) Sodium
|
68
|
+
12 => '1s² 2s² 2p⁶ 3s²', # (12) Magnesium
|
69
|
+
13 => '1s² 2s² 2p⁶ 3s² 3p¹', # (13) Aluminium
|
70
|
+
14 => '1s² 2s² 2p⁶ 3s² 3p²', # (14) Silicon
|
71
|
+
15 => '1s² 2s² 2p⁶ 3s² 3p³', # (15) Phosphorus
|
72
|
+
16 => '1s² 2s² 2p⁶ 3s² 3p⁴', # (16) Sulfur
|
73
|
+
17 => '1s² 2s² 2p⁶ 3s² 3p⁵', # (17) Chlorine
|
74
|
+
18 => '1s² 2s² 2p⁶ 3s² 3p⁶', # (18) Arsen
|
75
|
+
19 => '1s² 2s² 2p⁶ 3s² 3p⁶', # (19) Potassium
|
76
|
+
20 => '[Ar] 4s²', # (20) Calcium
|
77
|
+
21 => '[Ar] 4s² 3d¹', # (21) Scandium
|
78
|
+
22 => '[Ar] 4s² 3d²', # (22) Titanium
|
79
|
+
23 => '[Ar] 4s² 3d³', # (23) Vanadium
|
80
|
+
24 => '[Ar] 4s¹ 3d⁵', # (24) Chromium
|
81
|
+
25 => '[Ar] 4s² 3d⁵', # (25) Manganese
|
82
|
+
26 => '[Ar] 4s² 3d⁶', # (26) Iron
|
83
|
+
27 => '[Ar] 4s² 3d⁷', # (27) Cobalt
|
84
|
+
28 => '[Ar] 4s² 3d⁸', # (28) Nickel
|
85
|
+
29 => '[Ar] 4s¹ 3d¹⁰', # (29) Copper
|
86
|
+
30 => '[Ar] 4s² 3d¹⁰', # (30) Zinc
|
87
|
+
31 => '[Ar] 3d¹⁰ 4s² 4p¹', # (31) Gallium
|
88
|
+
32 => '[Ar] 3d¹⁰ 4s² 4p²', # (32) Germanium
|
89
|
+
33 => '[Ar] 3d¹⁰ 4s² 4p³', # (33) Arsenic
|
90
|
+
34 => '[Ar] 3d¹⁰ 4s² 4p⁴', # (34) Selenium
|
91
|
+
35 => '[Ar] 3d¹⁰ 4s² 4p⁵', # (35) Bromine
|
92
|
+
36 => '[Ar] 3d¹⁰ 4s² 4p⁶', # (36) Krypton
|
93
|
+
37 => '[Kr] 5s¹', # (37) Rubidium
|
94
|
+
38 => '[Kr] 5s²', # (38) Strontium
|
95
|
+
39 => '[Kr] 4d¹ 5s²', # (39) Yttrium
|
96
|
+
40 => '[Kr] 4d² 5s²', # (40) Zirconium
|
97
|
+
41 => '[Kr] 4d⁴ 5s¹', # (41) Niobium
|
98
|
+
42 => '[Kr] 4d⁵ 5s¹', # (42) Molybdenum
|
99
|
+
43 => '[Kr] 4d⁵ 5s²', # (43) Technetium
|
100
|
+
44 => '[Kr] 4d⁷ 5s¹', # (44) Ruthenium
|
101
|
+
45 => '[Kr] 4d⁸ 5s¹', # (45) Rhodium
|
102
|
+
46 => '[Kr] 4d¹⁰', # (46) Palladium
|
103
|
+
47 => '[Kr] 4d¹⁰ 5s¹', # (47) Silver
|
104
|
+
48 => '[Kr] 4d¹⁰ 5s²', # (48) Cadmium
|
105
|
+
49 => '[Kr] 4d¹⁰ 5s² 5p¹', # (49) Indium
|
106
|
+
50 => '[Kr] 4d¹⁰ 5s² 5p²', # (50) Tin
|
107
|
+
51 => '[Kr] 4d¹⁰ 5s² 5p³', # (51) Antimony
|
108
|
+
52 => '[Kr] 4d¹⁰ 5s² 5p⁴', # (52) Tellurium
|
109
|
+
53 => '[Kr] 4d¹⁰ 5s² 5p⁵', # (53) Iodine
|
110
|
+
54 => '[Kr] 4d¹⁰ 5s² 5p⁶', # (54) Xenon
|
111
|
+
55 => '[Xe] 6s¹', # (55) Caesium
|
112
|
+
}
|
113
|
+
|
114
|
+
_ = ChemistryParadise::Constants::FILE_PERIODIC_TABLE_OF_THE_ELEMENTS
|
115
|
+
|
116
|
+
# ========================================================================= #
|
117
|
+
# === PERIODIC_TABLE_DATASET
|
118
|
+
# ========================================================================= #
|
119
|
+
PERIODIC_TABLE_DATASET = YAML.load_file(_) if File.exist? _
|
120
|
+
|
121
|
+
# ========================================================================= #
|
122
|
+
# === initialize
|
123
|
+
# ========================================================================= #
|
124
|
+
def initialize(
|
125
|
+
optional_input = nil,
|
126
|
+
run_already = true
|
127
|
+
)
|
128
|
+
reset
|
129
|
+
set_input(optional_input)
|
130
|
+
run if run_already
|
131
|
+
end
|
132
|
+
|
133
|
+
# ========================================================================= #
|
134
|
+
# === reset
|
135
|
+
# ========================================================================= #
|
136
|
+
def reset
|
137
|
+
@n_electrons = 0
|
138
|
+
@_ = '' # The result string.
|
139
|
+
end
|
140
|
+
|
141
|
+
# ========================================================================= #
|
142
|
+
# === set_main_string
|
143
|
+
# ========================================================================= #
|
144
|
+
def set_main_string(i)
|
145
|
+
@_ = i
|
146
|
+
end
|
147
|
+
|
148
|
+
# ========================================================================= #
|
149
|
+
# === set_input
|
150
|
+
# ========================================================================= #
|
151
|
+
def set_input(i = N)
|
152
|
+
i = i.first if i.is_a? Array
|
153
|
+
i = DEFAULT_INPUT if i.nil?
|
154
|
+
i = i.to_s.dup.delete('/') # We don't need '/' characters.
|
155
|
+
i = periodic_table?.invert[i.to_i] if i =~ /^\d+$/
|
156
|
+
# ======================================================================= #
|
157
|
+
# Since as of June 2016, we will upcase the first character.
|
158
|
+
# ======================================================================= #
|
159
|
+
i = i.dup if i.frozen?
|
160
|
+
i[0,1] = i[0,1].upcase
|
161
|
+
@input = i
|
162
|
+
end
|
163
|
+
|
164
|
+
# ========================================================================= #
|
165
|
+
# === set_n_electrons
|
166
|
+
# ========================================================================= #
|
167
|
+
def set_n_electrons(i)
|
168
|
+
@n_electrons = i
|
169
|
+
end
|
170
|
+
|
171
|
+
# ========================================================================= #
|
172
|
+
# === periodic_table?
|
173
|
+
# ========================================================================= #
|
174
|
+
def periodic_table?
|
175
|
+
PERIODIC_TABLE_DATASET
|
176
|
+
end
|
177
|
+
|
178
|
+
# ========================================================================= #
|
179
|
+
# === default_colour
|
180
|
+
# ========================================================================= #
|
181
|
+
def default_colour
|
182
|
+
Colours::GREY
|
183
|
+
end
|
184
|
+
|
185
|
+
# ========================================================================= #
|
186
|
+
# === main_hash?
|
187
|
+
# ========================================================================= #
|
188
|
+
def main_hash?
|
189
|
+
MAIN_HASH
|
190
|
+
end
|
191
|
+
|
192
|
+
# ========================================================================= #
|
193
|
+
# === check_for_inclusion
|
194
|
+
# ========================================================================= #
|
195
|
+
def check_for_inclusion
|
196
|
+
if periodic_table?.has_key? @input
|
197
|
+
set_n_electrons periodic_table?[@input]
|
198
|
+
e rev+
|
199
|
+
'Found element '+sfancy(@input)+'. '+
|
200
|
+
'It has '+simp(@n_electrons.to_s)+' electrons.'
|
201
|
+
display_electron_configuration(@n_electrons)
|
202
|
+
else
|
203
|
+
e "Did not find element called `#{sfancy(@input)}`."
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
# ========================================================================= #
|
208
|
+
# === display_electron_configuration
|
209
|
+
#
|
210
|
+
# Show the specific electron configuration.
|
211
|
+
#
|
212
|
+
# For instance:
|
213
|
+
#
|
214
|
+
# 1s2 2s2 2p6 3s2 3p4
|
215
|
+
#
|
216
|
+
# ========================================================================= #
|
217
|
+
def display_electron_configuration(
|
218
|
+
i = @n_electrons
|
219
|
+
)
|
220
|
+
# ======================================================================= #
|
221
|
+
# Notify the user of invalid input.
|
222
|
+
# ======================================================================= #
|
223
|
+
unless main_hash?.has_key? i
|
224
|
+
e 'Note that the electron at position '+i.to_s+
|
225
|
+
' is not registered.'
|
226
|
+
end
|
227
|
+
set_main_string MAIN_HASH[i]
|
228
|
+
# ======================================================================= #
|
229
|
+
# Output it next:
|
230
|
+
# ======================================================================= #
|
231
|
+
e yellow(@_)
|
232
|
+
end
|
233
|
+
|
234
|
+
# ========================================================================= #
|
235
|
+
# === run (run tag)
|
236
|
+
# ========================================================================= #
|
237
|
+
def run
|
238
|
+
check_for_inclusion
|
239
|
+
end
|
240
|
+
|
241
|
+
end; end
|
242
|
+
|
243
|
+
if __FILE__ == $PROGRAM_NAME
|
244
|
+
include ChemistryParadise
|
245
|
+
if ARGV.first
|
246
|
+
ShowElectronConfiguration.new(ARGV.first)
|
247
|
+
else
|
248
|
+
ShowElectronConfiguration.new(ARGV)
|
249
|
+
end
|
250
|
+
end # $RUBY_CHEMISTRY/show_electron_configuration.rb
|
@@ -0,0 +1,145 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
# Encoding: UTF-8
|
3
|
+
# frozen_string_literal: true
|
4
|
+
# =========================================================================== #
|
5
|
+
# === ChemistryParadise::ShowElement
|
6
|
+
#
|
7
|
+
# Input should be something like this:
|
8
|
+
#
|
9
|
+
# show_element 'Xe+5'
|
10
|
+
#
|
11
|
+
# Usage example:
|
12
|
+
#
|
13
|
+
# ChemistryParadise::ShowElement.new(ARGV)
|
14
|
+
#
|
15
|
+
# =========================================================================== #
|
16
|
+
# require 'show_element'
|
17
|
+
# =========================================================================== #
|
18
|
+
require 'chemistry_paradise/base/base.rb'
|
19
|
+
|
20
|
+
module ChemistryParadise
|
21
|
+
|
22
|
+
class ShowElement < ::ChemistryParadise::Base # === ChemistryParadise::ShowElement
|
23
|
+
|
24
|
+
# ========================================================================= #
|
25
|
+
# === NAMESPACE
|
26
|
+
# ========================================================================= #
|
27
|
+
NAMESPACE = inspect
|
28
|
+
|
29
|
+
# ========================================================================= #
|
30
|
+
# === PERIODIC_TABLE
|
31
|
+
# ========================================================================= #
|
32
|
+
PERIODIC_TABLE = YAML.load_file(ChemistryParadise::Constants::FILE_PERIODIC_TABLE_OF_THE_ELEMENTS)
|
33
|
+
|
34
|
+
# ========================================================================= #
|
35
|
+
# === initialize
|
36
|
+
# ========================================================================= #
|
37
|
+
def initialize(
|
38
|
+
i = nil,
|
39
|
+
run_already = true
|
40
|
+
)
|
41
|
+
reset
|
42
|
+
set_input(i)
|
43
|
+
run if run_already
|
44
|
+
end
|
45
|
+
|
46
|
+
# ========================================================================= #
|
47
|
+
# === reset (reset tag)
|
48
|
+
# ========================================================================= #
|
49
|
+
def reset
|
50
|
+
set_name_of_the_element
|
51
|
+
set_relative_position
|
52
|
+
end
|
53
|
+
|
54
|
+
# ========================================================================= #
|
55
|
+
# === set_input
|
56
|
+
# ========================================================================= #
|
57
|
+
def set_input(i = '')
|
58
|
+
i = i.first if i.is_a? Array
|
59
|
+
i = i.to_s.dup if i
|
60
|
+
@input = i
|
61
|
+
process_input
|
62
|
+
end
|
63
|
+
|
64
|
+
# ========================================================================= #
|
65
|
+
# === set_relative_position
|
66
|
+
# ========================================================================= #
|
67
|
+
def set_relative_position(i = 0)
|
68
|
+
i = i.to_i if i
|
69
|
+
@relative_position = i
|
70
|
+
end
|
71
|
+
|
72
|
+
# ========================================================================= #
|
73
|
+
# === process_input
|
74
|
+
# ========================================================================= #
|
75
|
+
def process_input
|
76
|
+
if @input.include? '+'
|
77
|
+
set_batch('+')
|
78
|
+
elsif @input.include? '-'
|
79
|
+
set_batch('-')
|
80
|
+
else
|
81
|
+
set_name_of_the_element(@input)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
# ========================================================================= #
|
86
|
+
# === set_batch
|
87
|
+
#
|
88
|
+
# This will set both (1) the name and (2) the relative position.
|
89
|
+
# ========================================================================= #
|
90
|
+
def set_batch(on_which_token = '+')
|
91
|
+
_ = @input.split(on_which_token)
|
92
|
+
set_name_of_the_element _[0]
|
93
|
+
_[1] = '-'+_[1].to_s if on_which_token == '-'
|
94
|
+
set_relative_position _[1]
|
95
|
+
end
|
96
|
+
|
97
|
+
# ========================================================================= #
|
98
|
+
# === new_position?
|
99
|
+
# ========================================================================= #
|
100
|
+
def new_position?
|
101
|
+
@relative_position
|
102
|
+
end
|
103
|
+
|
104
|
+
# ========================================================================= #
|
105
|
+
# === report_result
|
106
|
+
# ========================================================================= #
|
107
|
+
def report_result
|
108
|
+
current_position = PERIODIC_TABLE[@name_of_the_element]
|
109
|
+
current_position = current_position + new_position?
|
110
|
+
opnn; e PERIODIC_TABLE.invert[current_position]+' is at position '+
|
111
|
+
sfancy(current_position.to_s)+'.'
|
112
|
+
end
|
113
|
+
|
114
|
+
# ========================================================================= #
|
115
|
+
# === set_name_of_the_element
|
116
|
+
# ========================================================================= #
|
117
|
+
def set_name_of_the_element(i = nil)
|
118
|
+
# ======================================================================= #
|
119
|
+
# if the input is a number, we invert the PERIODIC_TABLE.
|
120
|
+
# ======================================================================= #
|
121
|
+
if i =~ /^\d+$/
|
122
|
+
i = PERIODIC_TABLE.invert[i.to_i]
|
123
|
+
end
|
124
|
+
@name_of_the_element = i
|
125
|
+
end
|
126
|
+
|
127
|
+
# ========================================================================= #
|
128
|
+
# === run
|
129
|
+
# ========================================================================= #
|
130
|
+
def run # (run tag)
|
131
|
+
report_result
|
132
|
+
end
|
133
|
+
|
134
|
+
# ========================================================================= #
|
135
|
+
# === opnn
|
136
|
+
# ========================================================================= #
|
137
|
+
def opnn
|
138
|
+
super(NAMESPACE)
|
139
|
+
end
|
140
|
+
|
141
|
+
end; end
|
142
|
+
|
143
|
+
if __FILE__ == $PROGRAM_NAME
|
144
|
+
ChemistryParadise::ShowElement.new(ARGV)
|
145
|
+
end # show_element 'Xe+5'
|
@@ -0,0 +1,131 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
# Encoding: UTF-8
|
3
|
+
# frozen_string_literal: true
|
4
|
+
# =========================================================================== #
|
5
|
+
# require 'chemistry_paradise/sinatra/sinatra.rb'
|
6
|
+
# =========================================================================== #
|
7
|
+
require 'sinatra/base.rb'
|
8
|
+
|
9
|
+
module ChemistryParadise
|
10
|
+
|
11
|
+
class Sinatra < ::Sinatra::Base
|
12
|
+
|
13
|
+
require 'chemistry_paradise/toplevel_methods/misc.rb'
|
14
|
+
require 'chemistry_paradise/utility_scripts/calculate_atomic_mass.rb'
|
15
|
+
|
16
|
+
begin
|
17
|
+
require 'open_in_browser'
|
18
|
+
rescue LoadError; end
|
19
|
+
|
20
|
+
begin
|
21
|
+
require 'colours'
|
22
|
+
include Colours
|
23
|
+
rescue LoadError; end
|
24
|
+
|
25
|
+
begin
|
26
|
+
require 'html_tags'
|
27
|
+
include HtmlTags::BaseModule
|
28
|
+
rescue LoadError; end
|
29
|
+
|
30
|
+
# ========================================================================= #
|
31
|
+
# === USE_THIS_PORT
|
32
|
+
# ========================================================================= #
|
33
|
+
USE_THIS_PORT = '4567'
|
34
|
+
|
35
|
+
set :port, USE_THIS_PORT
|
36
|
+
|
37
|
+
# ========================================================================= #
|
38
|
+
# === initialize
|
39
|
+
# ========================================================================= #
|
40
|
+
def initialize
|
41
|
+
super()
|
42
|
+
reset
|
43
|
+
if ::ChemistryParadise.is_on_roebe?
|
44
|
+
require 'open_in_browser'
|
45
|
+
# ===================================================================== #
|
46
|
+
# Tell us which port will be used:
|
47
|
+
# ===================================================================== #
|
48
|
+
target = "http://localhost:#{USE_THIS_PORT}/"
|
49
|
+
e sfancy(target)
|
50
|
+
Thread.new {
|
51
|
+
Open.in_browser(target) {{ delay: '1.0 seconds' }}
|
52
|
+
}
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
# ========================================================================= #
|
57
|
+
# === reset
|
58
|
+
# ========================================================================= #
|
59
|
+
def reset
|
60
|
+
end
|
61
|
+
|
62
|
+
# ========================================================================= #
|
63
|
+
# === return_default_header_and_title
|
64
|
+
# ========================================================================= #
|
65
|
+
def return_default_header_and_title
|
66
|
+
'<html><title>'\
|
67
|
+
'ChemistryParadise Web-API'\
|
68
|
+
'</title><body>'
|
69
|
+
end
|
70
|
+
|
71
|
+
# =========================================================================== #
|
72
|
+
# === /compound
|
73
|
+
#
|
74
|
+
# Usage Examples:
|
75
|
+
#
|
76
|
+
# http://localhost:4567/compound/H2SO4
|
77
|
+
#
|
78
|
+
# =========================================================================== #
|
79
|
+
get('/compound/*'){
|
80
|
+
_ = params[:splat]
|
81
|
+
_ = _.first if _.is_a? Array
|
82
|
+
return_default_header_and_title+
|
83
|
+
p(
|
84
|
+
'Mass of '+_.to_s+': <b>'+
|
85
|
+
ChemistryParadise.compound_mass?(_).to_s+' ame</b>'
|
86
|
+
)+
|
87
|
+
'</body></html>'
|
88
|
+
}
|
89
|
+
# =========================================================================== #
|
90
|
+
# === /compound
|
91
|
+
# =========================================================================== #
|
92
|
+
get('/compound'){
|
93
|
+
return_default_header_and_title+
|
94
|
+
p(
|
95
|
+
'Please provide an argument to <b>compound/</b>.'
|
96
|
+
)+
|
97
|
+
'</body></html>'
|
98
|
+
}
|
99
|
+
|
100
|
+
# =========================================================================== #
|
101
|
+
# === /
|
102
|
+
#
|
103
|
+
# Usage Examples:
|
104
|
+
#
|
105
|
+
# http://localhost:4567/
|
106
|
+
#
|
107
|
+
# =========================================================================== #
|
108
|
+
get('/'){
|
109
|
+
return_default_header_and_title+
|
110
|
+
p(
|
111
|
+
"Available components:<br>\n"+
|
112
|
+
'<a href="compound/">compound/</a>'
|
113
|
+
)+
|
114
|
+
'</body></html>'
|
115
|
+
}
|
116
|
+
|
117
|
+
end
|
118
|
+
|
119
|
+
# =========================================================================== #
|
120
|
+
# === ChemistryParadise.start_sinatra_interface
|
121
|
+
# =========================================================================== #
|
122
|
+
def self.start_sinatra_interface
|
123
|
+
puts 'Trying to start the sinatra-interface of ChemistryParadise next.'
|
124
|
+
::ChemistryParadise::Sinatra.run!
|
125
|
+
end
|
126
|
+
|
127
|
+
end
|
128
|
+
|
129
|
+
if __FILE__ == $PROGRAM_NAME
|
130
|
+
ChemistryParadise.start_sinatra_interface
|
131
|
+
end # bioroebe --sinatra
|