Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

# -*- coding: utf-8 -*- 

""" 

@file 

@brief Helpers for the hackathon 2017 (Label Emmaüs). 

""" 

import os 

from io import StringIO 

import ijson 

from pyquickhelper.loghelper import noLOG 

 

 

def enumerate_json_items(filename, encoding=None, fLOG=noLOG): 

""" 

Enumerates items from a JSON file or string. 

 

@param filename filename or string or stream to parse 

@param encoding encoding 

@param fLOG logging function 

@return iterator on records at first level. 

 

It assumes the syntax follows the format: ``[ {"id":1, ...}, {"id": 2, ...}, ...]``. 

 

.. exref:: 

:title: Processes a json file by streaming. 

 

The module :epkg:`ijson` can read a JSON file by streaming. 

This module is needed because a record can be written on multiple lines. 

This function leverages it produces the following results. 

 

.. runpython:: 

:showcode: 

 

from ensae_projects.hackathon import enumerate_json_items 

 

text_json = ''' 

[ 

{ 

"glossary": { 

"title": "example glossary", 

"GlossDiv": { 

"title": "S", 

"GlossList": [{ 

"GlossEntry": { 

"ID": "SGML", 

"SortAs": "SGML", 

"GlossTerm": "Standard Generalized Markup Language", 

"Acronym": "SGML", 

"Abbrev": "ISO 8879:1986", 

"GlossDef": { 

"para": "A meta-markup language, used to create markup languages such as DocBook.", 

"GlossSeeAlso": ["GML", "XML"] 

}, 

"GlossSee": "markup" 

} 

}] 

} 

} 

}, 

{ 

"glossary": { 

"title": "example glossary", 

"GlossDiv": { 

"title": "S", 

"GlossList": { 

"GlossEntry": [{ 

"ID": "SGML", 

"SortAs": "SGML", 

"GlossTerm": "Standard Generalized Markup Language", 

"Acronym": "SGML", 

"Abbrev": "ISO 8879:1986", 

"GlossDef": { 

"para": "A meta-markup language, used to create markup languages such as DocBook.", 

"GlossSeeAlso": ["GML", "XML"] 

}, 

"GlossSee": "markup" 

}] 

} 

} 

} 

} 

] 

''' 

 

for item in enumerate_json_items(text_json): 

print('------------') 

print(item) 

""" 

if isinstance(filename, str): 

if "{" not in filename and os.path.exists(filename): 

with open(filename, "r", encoding=encoding) as f: 

for el in enumerate_json_items(f, encoding=encoding, fLOG=fLOG): 

yield el 

else: 

st = StringIO(filename) 

for el in enumerate_json_items(st, encoding=encoding, fLOG=fLOG): 

yield el 

else: 

parser = ijson.parse(filename) 

current = None 

curkey = None 

stack = [] 

nbyield = 0 

for i, (_, event, value) in enumerate(parser): 

if i % 1000000 == 0: 

fLOG("[enumerate_json_items] i={0} yielded={1}".format( 

i, nbyield)) 

if event == "start_array": 

if curkey is None: 

current = [] 

else: 

if not isinstance(current, dict): 

raise RuntimeError( 

"Type issue {0}".format(type(current))) 

c = [] 

current[curkey] = c 

current = c 

curkey = None 

stack.append(current) 

elif event == "end_array": 

stack.pop() 

if len(stack) == 0: 

# We should be done. 

current = None 

else: 

current = stack[-1] 

elif event == "start_map": 

c = {} 

if curkey is None: 

current.append(c) 

else: 

current[curkey] = c # pylint: disable=E1137 

stack.append(c) 

current = c 

curkey = None 

elif event == "end_map": 

stack.pop() 

current = stack[-1] 

if len(stack) == 1: 

nbyield += 1 

yield current[-1] 

# We clear the memory. 

current.clear() 

elif event == "map_key": 

curkey = value 

elif event in {"string", "number", "boolean"}: 

if curkey is None: 

current.append(value) 

else: 

current[curkey] = value # pylint: disable=E1137 

curkey = None 

elif event == "null": 

if curkey is None: 

current.append(None) 

else: 

current[curkey] = None # pylint: disable=E1137 

curkey = None 

else: 

raise ValueError("Unknown event '{0}'".format(event)) 

 

 

def extract_images_from_json_2017(filename, encoding=None, fLOG=noLOG): 

""" 

Extracts fields from a JSON files such as images. 

 

@param filename filename 

@param encoding encoding 

@param fLOG logging function 

@return iterator on images 

 

..warning:: Copy between two iterations? 

 

If you plan to store the enumerated dictionaries, you should 

copy them because dictionary are reused. 

 

One example on dummy data implementing a subset of the fields 

the JSON contains. This can be easily converted into a dataframe. 

 

.. runpython:: 

:showcode: 

 

from ensae_projects.hackathon import extract_images_from_json_2017 

 

text_json = ''' 

[ 

{"assigned_images": [], 

"best_offer": {"created_on": "2016-11-04T23:20:53+01:00", "images": [], "offer_longitude": null, "availability": "in_stock", 

"start_selling_date": null, "delay_before_shipping": 0.00, "free_return": null, "free_shipping": null, 

"assigned_images": [{"image_path": "https://coucou.JPEG"}], 

"id": 1306501, "eco_tax": 0.000000, "keywords": ["boutique", "test"], 

"sku": "AAAA27160018", 

"product": {"pk": 2550, "external_id": null, "id": 2580}, 

"description": "livre l", "last_modified": "2016-11-04T23:27:01+01:00", 

"name": "les names", "language": "fr"}, "id": 25540, 

"description": "livre 2", "slug": "les-l", 

"application_categories": [280, 283], "product_type": "physical", 

"name": "les l n", "language": "fr", "popularity": 99, "gender": null 

} 

] 

''' 

 

items = [] 

for item in extract_images_from_json_2017(text_json): 

print(item) 

items.append(item) 

 

from pandas import DataFrame 

df = DataFrame(items) 

print(df) 

""" 

for record in enumerate_json_items(filename, encoding=encoding, fLOG=fLOG): 

images = [] 

if "best_offer" in record and record["best_offer"]: 

best = record["best_offer"] 

if "assigned_images" in best and best["assigned_images"]: 

images.extend(best["assigned_images"]) 

else: 

continue 

product = best.get("product") 

if product is None: 

continue 

if "assigned_images" in record and record["assigned_images"]: 

images.extend(record["assigned_images"]) 

res = {} 

res["product_pk"] = product.get("pk") 

res["product_id"] = product.get("id") 

res["id2"] = record.get("id2") 

res["sku"] = best.get("sku") 

res["created_on"] = record.get("created_on") 

res["keywords"] = record.get("keywords") 

if isinstance(res["keywords"], list): 

res['keywords'] = ";".join(res['keywords']) 

res["availability"] = best.get("availability") 

res["eco_tax"] = best.get("eco_tax") 

res["restock_date"] = best.get("restock_date") 

res["status"] = best.get("status") 

res["number_of_items"] = best.get("number_of_items") 

res["price_with_vat"] = best.get("price_with_vat") 

res["price_without_vat"] = best.get("price_without_vat") 

res["previous_price_without_vat"] = best.get( 

"previous_price_without_vat") 

res["max_order_quantity"] = best.get("max_order_quantity") 

res["stock"] = best.get("stock") 

res["start_selling_date"] = best.get("start_selling_date") 

res["description"] = record.get("description") 

if isinstance(res["description"], str): 

res["description"] = res["description"].replace( 

"\n", "\\n").replace("\t", "\\t").replace("\r", "") 

res["last_modified"] = best.get("last_modified") 

res["name"] = record.get("name") 

res["product_type"] = record.get("product_type") 

res["gender"] = record.get("gender") 

res["popularity"] = record.get("popularity") 

res["application_categories"] = record.get("application_categories") 

if isinstance(res["application_categories"], list): 

res["application_categories"] = ",".join( 

map(str, res["application_categories"])) 

res["language"] = record.get("language") 

paths = list(im.get("image_path") for im in images) 

done = set() 

for p in paths: 

if p and p not in done: 

res["image_path"] = p 

yield res 

done.add(p)